问题
I plan to include a CardView
in my project. i have already included RecyclerView
and card view in my project. the problem is, i want to call for different activity for each card. i have implement different intent for each card. but it require me to initialize the data. this is my original code:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private Context context;
private String[] titles = {"Add new Research",
"View Your Research"};
private String[] details = {"Add your research files here",
"View all of your posted research"};
private int[] images = { R.drawable.add,
R.drawable.view};
class ViewHolder extends RecyclerView.ViewHolder{
public int currentItem;
public ImageView itemImage;
public TextView itemTitle;
public TextView itemDetail;
public ViewHolder(View itemView) {
super(itemView);
itemImage = (ImageView)itemView.findViewById(R.id.item_image);
itemTitle = (TextView)itemView.findViewById(R.id.item_title);
itemDetail =
(TextView)itemView.findViewById(R.id.item_detail);
itemView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
int position = getAdapterPosition();
Intent intent = new Intent(context, Choose.class);
if(position==0){
intent = new Intent(context, AddFiles.class);
}else if(position==1){
intent = new Intent(context, ViewFiles.class);
}
context.startActivity(intent);
}
});
}
}
when i click on the card view, it stated that my program are not responding.
even if i initialize intent as
Intent intent = null;
if(position==0){
intent = new Intent(context, AddFiles.class);
}else if(position==1){
intent = new Intent(context, ViewFiles.class);
}
context.startActivity(intent);
there still an error, what should i do? or is there better way to do it. this is my logcat error.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.mcormpelo, PID: 3645
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:128)
at android.content.Intent.<init>(Intent.java:4449)
at com.example.user.mcormpelo.RecyclerAdapter$ViewHolder$1.onClick(RecyclerAdapter.java:46)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
回答1:
You did not initialize the context
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private Context context;
public RecyclerAdapter(Context context) {
this.context = context;
}
Pass the Activity
reference from the caller activity.
sample usage
RecyclerAdapter rcAdapter = new RecyclerAdapter(MainActivity.this);
来源:https://stackoverflow.com/questions/41669479/how-to-include-different-intent-in-recyclerview