Intent is not working in my RecyclerView

后端 未结 4 836
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 00:17

I use this code:

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater         


        
4条回答
  •  猫巷女王i
    2021-01-27 01:00

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

    public void onClick(View v) {
                    Intent myIntent = new Intent(context, MainActivity2Activity.class);
                    context.startActivity(myIntent);
                }
    

    change the above code to:

    public void onClick(View v) {
                    Intent myIntent = new Intent(getApplicationContext(), MainActivity2Activity.class);
                    context.startActivity(myIntent);
                }
    

    or

    public void onClick(View v) {
                    Intent myIntent = new Intent(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:

    @Override
            public void onClick(View view) {
    
                if (clickListener != null) {
                    clickListener.itemClicked(view, getPosition());
                }
    
            }
    
    public interface ClickListener {
    
            public void itemClicked(View view, int position);
        }
    

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

    Hope this helps.

提交回复
热议问题