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

我正在开发一个在 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;

}



@Override

public 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);

}




@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() {

    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 项,因此出现错误。


这是错误的图像。


墨色风雨
浏览 105回答 3
3回答

呼唤远方

首先将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;}@Overridepublic UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType)&nbsp; &nbsp; &nbsp; &nbsp;{itemView = LayoutInflater.from(parent.getContext())&nbsp; &nbsp; &nbsp; &nbsp; .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() {return listUsers.size();}private void displayingAlertDialog() {&nbsp; &nbsp; //displaying alert dialog box&nbsp; &nbsp; AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());&nbsp; &nbsp; builder.setMessage("your toast message here...");&nbsp; &nbsp; builder.setCancelable(true);&nbsp; &nbsp; builder.setPositiveButton(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Ok",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new DialogInterface.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(DialogInterface dialog, int id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dialog.cancel();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; AlertDialog alert11 = builder.create();&nbsp; &nbsp; alert11.show();}/**&nbsp;* ViewHolder class&nbsp;*/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) {&nbsp; &nbsp; super(view);&nbsp; &nbsp; textViewID = (AppCompatTextView) view.findViewById(R.id.textViewID);&nbsp; &nbsp; textViewName = (AppCompatTextView) view.findViewById(R.id.textViewName);&nbsp; &nbsp; textViewPassword = (AppCompatTextView) view.findViewById(R.id.textViewPassword);&nbsp; &nbsp; textViewRole = (AppCompatTextView) view.findViewById(R.id.textViewRole);&nbsp; &nbsp; layout = view.findViewById(R.id.list_view);&nbsp; &nbsp; layout.setOnClickListener(this);}@Overridepublic void onClick(View v) {&nbsp; &nbsp; displayingAlertDialog();}}

月关宝盒

您尚未mContext在适配器类中声明。在 Adapter 类的构造函数中可能会像这样改变。public UserRecyclerAdapterSavedUsers(List<User> listUsers,Context context) {&nbsp; &nbsp; this.mContext= context;&nbsp; &nbsp; this.listUsers1 = listUsers;&nbsp; &nbsp; user= new User();}和你必须改变的回收视图活动类UserRecyclerAdapterSavedUsers myAdapter = new RecyclerViewAdapter(yourList,this);

守候你守候我

使用 Recyclerview 项目 click like this&nbsp;click here然后您可以访问您的界面activity或fragment然后您可以添加您需要的任何内容。在里面给予toast和填充不是正确的编码方式AlertDialogAdapter
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java