How can I call a new activity from an custom adapter in android application

Hello,
I tried to call an new activity from an custom adapter in android application. I used the below mention code:

// from the constructor of the class
public ChatRoomAdapter(Context context)
{
this.mContext = context;
}

// call activity from getview()
var intent = new Intent(context, typeof(MyNewActivity));
context.StartActivity(intent);

this is giving error as below:
Java.Lang.NullPointerException:

How can this be possible to call a activity from a adapter

Best Answers

  • soumenhaldersoumenhalder ✭✭
    Accepted Answer

    Hello @robbit ,
    Thanks for the advice.

    I have tried like following

    var intent = new Intent(mContext, typeof(activity));
    activity.StartActivity(intent);

    It is working fine.

Answers

  • soumenhaldersoumenhalder Member ✭✭

    Thank You @robbit for the reply.

    Yes that was a mistake. that was mContext but still I am getting Java.Lang.NullPointerException error. Here I am giving my code and it is under a button click event,

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
    // image button click event.
    ImageBtn.Click += (sender, args) =>
    {
    AlertDialog.Builder alert1 = new AlertDialog.Builder(activity);
    alert1.SetTitle("Message");
    alert1.SetMessage("View Image / Download Image");
    alert1.SetPositiveButton("Download", (senderAlert, args1) =>
    {
    // Download CODE
    });
    alert1.SetNegativeButton("View Image", (sender1, args2) =>
    {
    try
    {
    // here I wants to open a new activity.
    var intent = new Intent(mContext, typeof(MyNewActivity));
    mContext.StartActivity(intent);
    }
    catch(Exception ex)
    {
    }
    });
    Dialog dialog1 = alert1.Create();
    dialog1.Show();
    };
    }

    and MyNewActivity is calling another layout.

  • robbitrobbit CNMember Xamurai

    Hi @soumenhalder,

    By AlertDialog.Builder alert1 = new AlertDialog.Builder(activity);, can you show the initialization code about activity? I have changed it to mContext, that works fine.

  • soumenhaldersoumenhalder Member ✭✭

    hello @robbit ,
    I have written AlertDialog.Builder alert1 = new AlertDialog.Builder(mContext); it gives the same null reference error.

  • soumenhaldersoumenhalder Member ✭✭
    Accepted Answer

    Hello @robbit ,
    Thanks for the advice.

    I have tried like following

    var intent = new Intent(mContext, typeof(activity));
    activity.StartActivity(intent);

    It is working fine.

Sign In or Register to comment.