Skip to content Skip to sidebar Skip to footer

Intent Is Not Working In My Recyclerview

I use this code: import android.content.Context; import android.content.Intent; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.vi

Solution 1:

Probably context is null try it as:

publicvoidonClick(View v){
    Intent myIntent = newIntent(v.getContext(), MainActivity2Activity.class);
     v.getContext().startActivity(myIntent);
  }

Solution 2:

In the following code I feel context is null hence you get the error change the following code:

publicvoidonClick(View v){
                Intent myIntent = newIntent(context, MainActivity2Activity.class);
                context.startActivity(myIntent);
            }

change the above code to:

publicvoidonClick(View v){
                Intent myIntent = newIntent(getApplicationContext(), MainActivity2Activity.class);
                context.startActivity(myIntent);
            }

or

publicvoidonClick(View v){
                Intent myIntent = newIntent(getBaseContext, MainActivity2Activity.class);
                context.startActivity(myIntent);
            }

I feel what you are doing is not correct to use onClicks you need to set an interface as shown below:

@OverridepublicvoidonClick(View view) {

            if (clickListener != null) {
                clickListener.itemClicked(view, getPosition());
            }

        }

publicinterfaceClickListener {

        publicvoiditemClicked(View view, int position);
    }

Now you need to implement the interface in your activity that will work fine. Try this.

Hope this helps.

Solution 3:

AFAIK, RecyclerView doesn't support click events of child views of items. Any events of child views are shielded. Only RecyclerView.OnItemTouchListener is OK to listen to events of items of RecyclerView.

Solution 4:

Hi call your adapter such like this way :

 adapter = newMyAdapter(MainActivity.this,people);
 recyclerView.setAdapter(adapter);

You can also get the view.getContext() inside you button onClick listener.

I hope it can help you to resolved the problem. thank you.

Post a Comment for "Intent Is Not Working In My Recyclerview"