使用intent从我的ViewHolder内部启动活动会使应用程序崩溃

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用intent从我的ViewHolder内部启动活动会使应用程序崩溃相关的知识,希望对你有一定的参考价值。

我试图使用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) 

任何帮助都感激不尽。

答案

首先要做的事情。当您遇到这些问题时,请发布崩溃日志,以便我们更准确地帮助您。

让我直截了当,你想在点击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);

    }
 }
另一答案

使用你上面的mCtx

            mCtx.startActivity(new Intent(mCtx, NoteEdit.class));

编辑:

添加logcat后。问题不在你的活动中。这是第二次活动

另一答案

您不应该从Adapter启动活动。适配器应该只处理数据集中每个项目的视图。所有与适配器中的项目视图相关的内容都可以在其中完成,但不能再进行。

您需要使用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);

以上是关于使用intent从我的ViewHolder内部启动活动会使应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

如何关闭从我的应用启动的外部应用(如 Instagram)并返回触发应用?

NestedScrollView内部的RecyclerView ViewHolder创建

文本未使用 Intent 传递给 Whatsapp 应用程序

RecyclerView ViewHolder 的新活动卡在黑屏上

每当我想从我的班级运行 Intent 时,NPE [重复]

RecyclerView.ViewHolder 是不是总是必须是内部类?