来回切换片段后的Recyclerview上的Android Nullpointerexception

时间:2016-04-06 05:58:49

标签: java android android-fragments android-recyclerview

我有一个带有几个片段的导航抽屉应用程序。其中一个使用recyclerviewadapter / recyclerviewholder来显示项目。在片段之间切换效果很好但是在切换之后,单击带有侦听器的项会导致nullpointerexception。现在,我在没有听众的情况下测试它,它工作正常。我猜这个听众没有刷新?当片段被刷?有人可以帮帮我吗?一直在寻找,但我无法解决它。

  

流程:com.mikepenz.materialdrawer.app.debug,PID:30803                                                                                          java.lang.NullPointerException:尝试调用接口方法   “无效   com.mikepenz.materialdrawer.app.RecyclerViewAdapter $ OnItemClickListener.onItemClick(java.lang.String中)”   在null对象引用上                                                                                              在   com.mikepenz.materialdrawer.app.RecyclerViewAdapter $ 1.onClick(RecyclerViewAdapter.java:43)                                                                                              在android.view.View.performClick(View.java:5198)                                                                                              在android.view.View $ PerformClick.run(View.java:21147)                                                                                              在android.os.Handler.handleCallback(Handler.java:739)                                                                                              在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                              在android.os.Looper.loop(Looper.java:148)                                                                                              在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                              at java.lang.reflect.Method.invoke(Native Method)                                                                                              在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                              在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {

private List<ItemObject> itemList;
private Context context;
private OnItemClickListener listener;


public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
    this.itemList = itemList;
    this.context = context;
}

@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
    RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView, context);
    return rcv;
}

@Override
public void onBindViewHolder(RecyclerViewHolders holder, final int position) {
    holder.countryName.setText(itemList.get(position).getName());
//    holder.countryPhoto.setImageResource(itemList.get(position).getPhoto());

    holder.countryName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
      //    listener.onItemClick(itemList.get(position).getName());

            Log.d("HELLO   ",itemList.get(position).getName());
            if (position ==1)
            {
                listener.onItemClick("GA");
            }
        }
    });

}


public interface OnItemClickListener{
     void onItemClick(String textName);
}

public void setOnItemClickListener(OnItemClickListener listener){
    this.listener = listener;
}


@Override
public int getItemCount() {
    Log.e("itemlist size ", this.itemList.size() + "");
    return this.itemList.size();
}

}

1 个答案:

答案 0 :(得分:-2)

替换以下代码

   listener.onItemClick("GA");

if(listener!=null){
   listener.onItemClick("GA");
     }

它可能适合您,当您尝试设置onItemClick

时,可能是您的侦听器null