我正在尝试使用Intent
调用另一个活动 - 代码会生成一个卡片列表,我希望用户能够通过单击来编辑卡片。
其余代码工作正常但是当我点击一张卡时应用程序崩溃。
public class NoteCardAdapter extends RecyclerView.Adapter<NoteCardAdapter.NoteCardViewHolder> {
private Context mCtx;
private List<NoteCard> noteCardList;
public NoteCardAdapter(Context mCtx, List<NoteCard> noteCardList) {
this.mCtx = mCtx;
this.noteCardList = noteCardList;
}
@Override
public NoteCardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.note_layout, null);
return new NoteCardViewHolder(view);
}
@Override
public void onBindViewHolder(final NoteCardViewHolder holder, int position) {
final NoteCard noteCard = noteCardList.get(position);
holder.noteCardTitle.setText(noteCard.getNoteCardTitle());
holder.noteCardSummary.setText(noteCard.getNoteCardSummary());
}
@Override
public int getItemCount() {
return noteCardList.size();
}
class NoteCardViewHolder extends RecyclerView.ViewHolder {
public TextView noteCardTitle, noteCardSummary;
public LinearLayout linearLayout;
private AdapterView.OnItemClickListener itemClickListener;
public NoteCardViewHolder(final View itemView) {
super(itemView);
noteCardTitle = itemView.findViewById(R.id.noteTitleView);
noteCardSummary = itemView.findViewById(R.id.noteSummaryView);
linearLayout = itemView.findViewById(R.id.linearLayout);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//THIS IS WHERE THE APP CRASHES
itemView.getContext().startActivity(new Intent(itemView.getContext(), NoteEdit.class));
}
});
}
}
}
崩溃日志显示:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dlimited.mydolist, PID: 4007
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dlimited.mydolist/com.dlimited.mydolist.NoteEdit}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.dlimited.mydolist.NoteEdit.onCreate(NoteEdit.java:31)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
非常感谢任何帮助。
答案 0 :(得分:1)
首先要做的事情。当您遇到这类问题时,请发布崩溃的日志,以便我们更准确地为您提供帮助。
让我直截了当,你想在点击Recyclerview的列表时调用一个活动,对吧?是的,它应该有效,如果你这样做的话。您可以使用ViewHolder实现单击侦听器并覆盖onClick方法..
class NoteCardViewHolder extends RecyclerView.ViewHolder
iplements View.onClickListener {
....
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), YourActivity.class);
startActivity(intent);
}
}
答案 1 :(得分:1)
使用您上面的mCtx
。
mCtx.startActivity(new Intent(mCtx, NoteEdit.class));
修改强>
添加logcat后。问题不在你的活动中。它是在第二个活动
答案 2 :(得分:1)
您不应该从适配器启动活动。 适配器应仅处理数据集中每个项目的视图。所有与适配器中的项目视图相关的内容都可以在其中完成,但不能再进行。
您需要使用Callback / Listener来告诉Adapter驻留的Activity或Fragment以启动Activity。
向适配器添加一个侦听器,如下所示:
public class NoteCardAdapter extends RecyclerView.Adapter<NoteCardAdapter.NoteCardViewHolder> {
private Listener mListener;
public interface Listener {
void itemClicked();
}
...
public NoteCardAdapter(Listener, listener, List<NoteCard> noteCardList) {
this.mListener = listener;
this.noteCardList = noteCardList;
}
...
class NoteCardViewHolder extends RecyclerView.ViewHolder {
...
public NoteCardViewHolder(final View itemView) {
super(itemView);
...
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mListener.itemClicked();
}
});
}
}
...
}
然后在您的Activity调用者中,您需要为Adapter实现侦听器:
NoteCardAdapter.Listener listener = new NoteCardAdapter.Listener() {
@Override public void onItemClicked() {
// start the activity here.
startActivity(new Intent(YourActivity.this, NoteEdit.class));
}
});
NoteCardAdapter adapter = new NoteCardAdapter(listener, noteCardList);