Recyclerview中的对话框onClick

时间:2018-10-12 12:39:33

标签: android android-recyclerview

我试图做RecyclerView列表,当我单击片段时,我需要显示带有OnclickListeners的Dialog(我有两个按钮),但我正在获取Null对象引用。我试图使if(Dialog.isShowing())但仍然无法正常工作。

我的按钮 enter image description here

我的Logcat

Process: com.dontknow.myapplication231, PID: 4690
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.dontknow.myapplication231.Driver.RecyclerViewAdapter$2.onClick(RecyclerViewAdapter.java:84)
    at android.view.View.performClick(View.java:5716)
    at android.view.View$PerformClick.run(View.java:22596)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:7325)
    at java.lang.reflect.Method.invoke(Native Method)

这就是我的RecyclerViewAdapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

private Context mContext;
private List<Driver_Buttons_Data> mData;
private ProgressDialog progressDialog;
private Dialog myDialog;
private Button accept;
private Button decline;
private RadioGroup radioGroup;

RecyclerViewAdapter(Context mContext, List<Driver_Buttons_Data> mData){
    this.mContext = mContext;
    this.mData = mData;
}


@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    final View view = LayoutInflater.from(mContext).inflate(R.layout.button_driver_layout,viewGroup,false);
    final MyViewHolder viewHolder = new MyViewHolder(view);

    viewHolder.image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(mContext,Driver_maps.class);
            mContext.startActivity(intent);
        }
    });

    //Dialog creating
    myDialog = new Dialog(mContext);
    myDialog.setContentView(R.layout.choose_location);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        myDialog.create();
    }

    //accept and decline
    viewHolder.item.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                myDialog.show();
                if(myDialog.isShowing()){
                radioGroup = v.findViewById(R.id.location_group);

                accept = v.findViewById(R.id.accept);
                accept.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int id = radioGroup.getCheckedRadioButtonId();
                        switch (id){
                            case R.id.street1:
                                new Driver_maps(Dubnivska);
                                break;
                            case R.id.street2:
                                new Driver_maps(Pyrogova);
                                break;
                            case R.id.street3:
                                new Driver_maps(BogdanHmelnytskiy);
                                break;
                            case R.id.street4:
                                new Driver_maps(Kovelska);
                                break;
                        }
//                            if (FragmentMyOffer.getSizelist()==0){
//                              
((FragmentActivity)v.getContext()).getFragmentManager().beginTransaction()
//                                        
.replace(R.id.recycler_my_offer,FragmentMyOffer.getFragment()).commit();
//                            }
                    }
                });
            }
        }
    });


    return viewHolder;
}



@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int i) {
    holder.tv_a.setText("Offer");
    holder.tv_b.setText(Driver_Buttons_Data.getTxt_b());
    holder.image.setImageResource(mData.get(i).getImage());
}


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

static class MyViewHolder extends RecyclerView.ViewHolder{

    private TextView tv_a;
    private TextView tv_b;

    private ImageView image;
    private Button accept;
    private Button decline;
    private LinearLayout item;


    MyViewHolder(@NonNull View itemView) {
        super(itemView);

        tv_a = itemView.findViewById(R.id.txtText);
        tv_b = itemView.findViewById(R.id.txtSubText);
        image = itemView.findViewById(R.id.imgThumb);
        item = itemView.findViewById(R.id.recycler_item);
    }
}

我的问题是:如何在RecyclerView的Dialog中创建onClickListeners? 感谢您的帮助。

2 个答案:

答案 0 :(得分:0)


./config --static -static

可以显示项目视图的布局吗?它有两个按钮吗?

      Its crashing here.
      accept.setOnClickListener(new View.OnClickListener()

看起来您正在接受对话框布局中显示的按钮。

item = itemView.findViewById(R.id.recycler_item);

我的建议是不要在创建超时时创建ononViewViewHolde对话框,而是可以创建ondemand对话框。

            You need to get the view reference like this.
            accept = myDialog.findViewById(R.id.accept); 

答案 1 :(得分:0)

尝试一下:

  viewHolder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //move Dialog creating to here
               myDialog = new Dialog(mContext);
               myDialog.setContentView(R.layout.choose_location);
               if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                   myDialog.create();
               }
               myDialog.show();
               ...........

用于创建对话框的代码应放置在与显示对话框相同的位置。