为了账号安全,请及时绑定邮箱和手机立即绑定

NULL 指针异常错误应用程序在单击回收器列表项后崩溃

NULL 指针异常错误应用程序在单击回收器列表项后崩溃

墨色风雨 2022-06-15 17:06:18
我正在开发一个在 Recycle List View Holder 中显示数据的 android 应用程序。当我单击 Recycler View Holder 中的列表项时,应用程序崩溃。public class UserRecyclerAdapterSavedUsers extends RecyclerView.Adapter<UserRecyclerAdapterSavedUsers.UserViewHolder> {private List<User> listUsers;Context mContext;ItemClickListenerLongPressed itemClickListenerLongPressed;public UserRecyclerAdapterSavedUsers(List<User> listUsers) {    this.listUsers = listUsers;}@Overridepublic UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {    View itemView = LayoutInflater.from(parent.getContext())            .inflate(R.layout.item_user_recycler_second, parent, false);    return new UserViewHolder(itemView);}@Overridepublic void onBindViewHolder(UserViewHolder holder, int position) {    holder.textViewID.setText(listUsers.get(position).getUserid());    holder.textViewName.setText(listUsers.get(position).getName());    holder.textViewPassword.setText(listUsers.get(position).getPassword());    holder.textViewRole.setText(listUsers.get(position).getRole());}public void setItemClickListenerLongPressed(ItemClickListenerLongPressed itemClickListenerLongPressed){    this.itemClickListenerLongPressed=itemClickListenerLongPressed;}@Overridepublic int getItemCount() {    Log.v(UserRecyclerAdapterSavedUsers.class.getSimpleName(),""+listUsers.size());    return listUsers.size();}/** * ViewHolder class */public class UserViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {    //public AppCompatTextView ID;    public AppCompatTextView textViewID;    public AppCompatTextView textViewName;    public AppCompatTextView textViewPassword;    public AppCompatTextView textViewRole;    }}}当我单击列表项时,它崩溃了,错误是由 Toast 引起的。当我删除 toast 时,由于使用了未单击的 try catch 项,因此出现错误。这是错误的图像。
查看完整描述

3 回答

?
呼唤远方

TA贡献1856条经验 获得超11个赞

首先将id添加到您的父 LinearLayout 为android:id="@+id/list_view"


然后更新适配器类


public class UserRecyclerAdapterSavedUsers extends RecyclerView.Adapter<UserRecyclerAdapterSavedUsers.UserViewHolder> {


private List<User> listUsers;

Context mContext;

ItemClickListenerLongPressed itemClickListenerLongPressed;

View itemView;


public UserRecyclerAdapterSavedUsers(List<User> listUsers) {

this.listUsers = listUsers;

}



@Override

public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType)       {

itemView = LayoutInflater.from(parent.getContext())

        .inflate(R.layout.item_user_recycler_second, parent, false);


return new UserViewHolder(itemView);

}




@Override

public void onBindViewHolder(UserViewHolder holder, int position) {

holder.textViewID.setText(listUsers.get(position).getUserid());

holder.textViewName.setText(listUsers.get(position).getName());

holder.textViewPassword.setText(listUsers.get(position).getPassword());

holder.textViewRole.setText(listUsers.get(position).getRole());


}


public void setItemClickListenerLongPressed(ItemClickListenerLongPressed itemClickListenerLongPressed){

this.itemClickListenerLongPressed=itemClickListenerLongPressed;

}


@Override

public int getItemCount() {

return listUsers.size();

}


private void displayingAlertDialog() {

    //displaying alert dialog box

    AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());

    builder.setMessage("your toast message here...");

    builder.setCancelable(true);


    builder.setPositiveButton(

            "Ok",

            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int id) {

                    dialog.cancel();


                }

            });


    AlertDialog alert11 = builder.create();

    alert11.show();

}


/**

 * ViewHolder class

 */

public class UserViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


//public AppCompatTextView ID;

public AppCompatTextView textViewID;

public AppCompatTextView textViewName;

public AppCompatTextView textViewPassword;

public AppCompatTextView textViewRole;

LinearLayout layout;


public UserViewHolder(View view) {

    super(view);


    textViewID = (AppCompatTextView) view.findViewById(R.id.textViewID);

    textViewName = (AppCompatTextView) view.findViewById(R.id.textViewName);

    textViewPassword = (AppCompatTextView) view.findViewById(R.id.textViewPassword);

    textViewRole = (AppCompatTextView) view.findViewById(R.id.textViewRole);

    layout = view.findViewById(R.id.list_view);


    layout.setOnClickListener(this);

}


@Override

public void onClick(View v) {

    displayingAlertDialog();

}

}


查看完整回答
反对 回复 2022-06-15
?
月关宝盒

TA贡献1772条经验 获得超5个赞

您尚未mContext在适配器类中声明。在 Adapter 类的构造函数中可能会像这样改变。


public UserRecyclerAdapterSavedUsers(List<User> listUsers,Context context) {

    this.mContext= context;

    this.listUsers1 = listUsers;

    user= new User();

}

和你必须改变的回收视图活动类


UserRecyclerAdapterSavedUsers myAdapter = new RecyclerViewAdapter(yourList,this);


查看完整回答
反对 回复 2022-06-15
?
守候你守候我

TA贡献1802条经验 获得超10个赞

使用 Recyclerview 项目 click like this click here

然后您可以访问您的界面activityfragment然后您可以添加您需要的任何内容。

  • 在里面给予toast和填充不是正确的编码方式AlertDialogAdapter


查看完整回答
反对 回复 2022-06-15
  • 3 回答
  • 0 关注
  • 104 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信