Recyclerview适配器中列表器上的空对象引用错误

时间:2018-10-09 11:24:23

标签: android android-recyclerview

我为我的回收站视图创建了一个适配器,并创建了一个带有删除功能的接口,但是当我运行该应用程序并没有任何反应时,通过调试我知道侦听器为null。我在另一个类中定义了接口函数文件。 我不知道发生了什么。有人可以帮忙吗?

适配器:

    public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.NotificationViewHolder> {

    private ArrayList<NotificationItem> mNotificationList;
    private onItemClickListner mListner;

    private Context myContext;
    private int layoutResID;

   public interface onItemClickListner{
     void onItemClick(int position);
     void onDeleteClick(int position);
    }

//    public void setOnItemClickListner(onItemClickListner listner){
//       mListner=listner;
//    }

    //NOTIFICATION HOLDER
    public static class NotificationViewHolder extends RecyclerView.ViewHolder{
        public TextView mNotificationTextView;
        public RelativeLayout mNotificaionHolderLayout;
        public ImageView imageDelete;
        onItemClickListner listner;


        public NotificationViewHolder(View itemView,final onItemClickListner listner) {
            super(itemView);
            mNotificationTextView=itemView.findViewById(R.id.NotificationTextView);
            mNotificaionHolderLayout=itemView.findViewById(R.id.notification__item_container);
            imageDelete=itemView.findViewById(R.id.notification_delete_image);
            this.listner=listner;


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   if (listner!=null){
                       int position=getAdapterPosition();
                       if (position!=RecyclerView.NO_POSITION){
                           listner.onItemClick(position);
                       }
                   }
                }
            });

            imageDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listner!=null){
                        int position=getAdapterPosition();
                        if (position!=RecyclerView.NO_POSITION){
                            listner.onDeleteClick(position);
                        }
                    }
                }
            });
        }
    }//NOTIFICATION HOLDER

    public NotificationAdapter(Context context, int resource, ArrayList<NotificationItem> notificationList,onItemClickListner listner){
        myContext=context;
        this.layoutResID=resource;
        mNotificationList=notificationList;
        this.mListner=listner;
    }


    @Override
    public NotificationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_notificationitem,parent,false);
        NotificationViewHolder evh=new NotificationViewHolder(v,mListner);
        return evh;
    }

    @Override
    public void onBindViewHolder(NotificationViewHolder holder, int position) {
        NotificationItem currentItem=mNotificationList.get(position);
        holder.mNotificaionHolderLayout.setBackgroundColor(Color.parseColor(Getcolor(position)));
        holder.mNotificationTextView.setText(currentItem.getNotifi_Name());
    }

    @Override
    public int getItemCount() {
        return mNotificationList.size();
    }

    private String Getcolor(int position)
    {
        String clr;

        switch (position)
        {
            case 0:
                clr="#FF9966";
                break;

            case 1:
                clr="#009900";
                break;

            case 2:
                clr="#006699";
                break;

            case 3:
                clr="#751947";
                break;
            case 4:
                clr="#FF9966";
                break;

            case 5:
                clr="#009900";
                break;

            case 6:
                clr="#006699";
                break;

            case 7:
                clr="#751947";
                break;


            default:
                clr="#FFA500";
                break;
        }
        return clr;
    }

}

LayoutFile:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
    android:id="@+id/notification__item_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="7dp"
    android:paddingBottom="7dp">
    <TextView
        android:id="@+id/NotificationTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="320dp"
        android:minWidth="320dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"/>

    <ImageView
        android:id="@+id/notification_delete_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:paddingRight="2dp"
        android:layout_alignParentRight="true"
        android:src="@drawable/close1"
        android:paddingTop="5dp"
        />
</RelativeLayout>

  

这是从我的家庭活动类中初始化监听器的

  @Override
public void onItemClick(int position) {
    FlashMessage("Clicked");

}

@Override
public void onDeleteClick(int position) {
       FlashMessage("DELETED ON POSITION : " +position);
       System.out.print("onDeleteClick");
       listNotifi.remove(position);
       nfAdapter.notifyItemRemoved(position);

}
  

适配器家庭活动

  NotificationAdapter nfAdapter=new NotificationAdapter(Home.this,R.layout.listview_notification_item, listNotifi,this);
            lvNotification.setAdapter(nfAdapter);

4 个答案:

答案 0 :(得分:0)

In your code :
if (listner==null){
                    int position=getAdapterPosition();
                    if (position!=RecyclerView.NO_POSITION){
                        listner.onDeleteClick(position);
                    }
                }
            }

Why you are checking for null and in fact You need to check if (listner!=null){}

答案 1 :(得分:0)

在初始化notificationAdapter之后,将以下代码放在ActivityFragment中。

notificationAdapter.setOnClickListener(new NotificationAdapter.onItemClickListner(){

    void onItemClick(int position){

    }
    void onDeleteClick(int position){

    }
});

答案 2 :(得分:0)

您没有在活动或实现您的方法的片段中实现侦听器。

  public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.NotificationViewHolder> {

private ArrayList<NotificationItem> mNotificationList;
private onItemClickListner mListner;

private Context myContext;
private int layoutResID;
String eduflexURL="https://eduflex.co.in/portal/";

public interface onItemClickListner{
 void onItemClick(int position);
 void onDeleteClick(int position);
}
//remove this no need
//public void setOnItemClickListner(onItemClickListner listner){
  // mListner=listner;
 // }

//NOTIFICATION HOLDER
public static class NotificationViewHolder extends RecyclerView.ViewHolder{
    public TextView mNotificationTextView;
    public RelativeLayout mNotificaionHolderLayout;
    public ImageView imageDelete;
    onItemClickListner listner;


    public NotificationViewHolder(View itemView,final onItemClickListner listner) {
        super(itemView);
        mNotificationTextView=itemView.findViewById(R.id.NotificationTextView);
        mNotificaionHolderLayout=itemView.findViewById(R.id.notification__item_container);
        imageDelete=itemView.findViewById(R.id.notification_delete_image);
        this.listner=listner;


        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if (listner!=null){
                   int position=getAdapterPosition();
                   if (position!=RecyclerView.NO_POSITION){
                       mListner.onItemClick(position);
                   }
               }
            }
        });

        imageDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listner!=null){
                    int position=getAdapterPosition();
                    if (position!=RecyclerView.NO_POSITION){
                        mListner.onDeleteClick(position);
                    }
                }
            }
        });
    }
}//NOTIFICATION HOLDER

//在此处将您的接口添加到构造器中

 public NotificationAdapter(Context context, int resource, 
    ArrayList<NotificationItem> 
 notificationList,onItemClickListner mListener){
    myContext=context;
    this.layoutResID=resource;
    mNotificationList=notificationList;
    this.mListener = mListener
}


@Override
public NotificationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_notificationitem,parent,false);
    NotificationViewHolder evh=new NotificationViewHolder(v,mListner);
    return evh;
}

@Override
public void onBindViewHolder(NotificationViewHolder holder, int position) {
    NotificationItem currentItem=mNotificationList.get(position);
    holder.mNotificaionHolderLayout.setBackgroundColor(Color.parseColor(Getcolor(position)));
    holder.mNotificationTextView.setText(currentItem.getNotifi_Name());
}

@Override
public int getItemCount() {
    return mNotificationList.size();
}

private String Getcolor(int position)
{
    String clr;

    switch (position)
    {
        case 0:
            clr="#FF9966";
            break;

        case 1:
            clr="#009900";
            break;

        case 2:
            clr="#006699";
            break;

        case 3:
            clr="#751947";
            break;
        case 4:
            clr="#FF9966";
            break;

        case 5:
            clr="#009900";
            break;

        case 6:
            clr="#006699";
            break;

        case 7:
            clr="#751947";
            break;


        default:
            clr="#FFA500";
            break;
    }
    return clr;
}

}

在您的活动或片段中实现

   public class yourActivity extends Activity implements 
                  NotificationAdapter.onItemClickListner{


       //Add the Override method


   }

呼叫适配器

  NotificationAdapter madapter = new 

  NotificationAdapter(YourActivity.this,resId,list,this)

答案 3 :(得分:0)

您忘记了调用 setOnItemClickListner ,或者您必须在 NotificationAdapter 构造函数并分配给mListner

例如

NotificationAdapter adapter=new NotificationAdapter(context,res,list,"Pass your listerner here");

在适配器中

 public NotificationAdapter(Context context, int resource, ArrayList<NotificationItem> notificationList,onItemClickListner listener){
        myContext=context;
        this.layoutResID=resource;
        mNotificationList=notificationList;
this.mListner=listener;
    }