如何在DialogAlert中制作RecyclerView

时间:2017-01-26 03:14:02

标签: android android-recyclerview alertdialog

我的DialogAlert中的RecyclerView存在问题。我有自定义对话警报,如下所示:

First Dialog

Second Dialog Solved

我的第一个DialogAlert工作正常,因为它只显示一些editText。但是当我点击图像信息(在editText中留下可绘制的)时,我有一些错误。它始终强制停止。我看过thisthis,但它不起作用。这是我的日志:

E/InputEventReceiver: Exception dispatching input event.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
                    at com.example.development_laptop.vhp_restotemp.TableActivityOnline$11.onTouch(TableActivityOnline.java:412)
                    at android.view.View.dispatchTouchEvent(View.java:9290)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                    at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
                    at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
                    at android.app.Dialog.dispatchTouchEvent(Dialog.java:787)
                    at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
                    at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
                    at android.view.View.dispatchPointerEvent(View.java:9514)
                    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
                    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
                    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
                    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
                    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
                    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
                    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)
                    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)
                    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                    at android.os.MessageQueue.nativePollOnce(Native Method)
                    at android.os.MessageQueue.next(MessageQueue.java:323)
                    at android.os.Looper.loop(Looper.java:135)
                    at android.app.ActivityThread.main(ActivityThread.java:5417)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.development_laptop.vhp_restotemp, PID: 4011
              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
                  at com.example.development_laptop.vhp_restotemp.TableActivityOnline$11.onTouch(TableActivityOnline.java:412)
                  at android.view.View.dispatchTouchEvent(View.java:9290)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
                  at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
                  at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
                  at android.app.Dialog.dispatchTouchEvent(Dialog.java:787)
                  at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
                  at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
                  at android.view.View.dispatchPointerEvent(View.java:9514)
                  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
                  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
                  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
                  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
                  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
                  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
                  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)
                  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)
                  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                  at android.os.MessageQueue.nativePollOnce(Native Method)
                  at android.os.MessageQueue.next(MessageQueue.java:323)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                at com.android.internal.os.ZygoteInit.mai

这是我的Activity.java中的DialogAlert代码:

public void openTableActivityDialog() {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.open_table_activity, null);
    dialogBuilder.setView(dialogView);

    final EditText edt_guestName_OpenActivity = (EditText) dialogView.findViewById(R.id.edt_guestname_openActivity);
    final EditText edt_pax_OpenActivity = (EditText) dialogView.findViewById(R.id.edt_pax_openActivity);
    final EditText edt_room_OpenActivity = (EditText) dialogView.findViewById(R.id.edt_room_openActivity);

    edt_room_OpenActivity.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    final int DRAWABLE_RIGHT = 2;
                    if(event.getAction() == MotionEvent.ACTION_UP) {
                        if(event.getRawX() >= (edt_room_OpenActivity.getRight() - edt_room_OpenActivity.
                                getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {

                            AlertDialog.Builder dialogBuilderRoomList = new AlertDialog.Builder(TableActivityOnline.this);
                            LayoutInflater inflater = TableActivityOnline.this.getLayoutInflater();
                            final View dialogView = inflater.inflate(R.layout.guestname_list_activity, null);
                            dialogBuilderRoomList.setView(dialogView);

                            filteredGuestRoomList = new JSONArray();

                            final GenericAdapter adapterGuestNameList;

                            final RecyclerView recyclerViewGuestNameList = (RecyclerView) dialogView.findViewById(R.id.recyclerViewGuestNameList);
                            final LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(TableActivityOnline.this);
                            recyclerViewGuestNameList.setLayoutManager(mLinearLayoutManager);

                            loadGuestRoom();

                            adapterGuestNameList = new GenericAdapter(recyclerViewGuestNameList);
                            recyclerViewGuestNameList.setAdapter(adapterGuestNameList);


                            adapterGuestNameList.setOnRecyclerViewListener(new GenericAdapter.OnRecyclerViewListener() {
                                @Override
                                public void OnRecyclerViewItemClicked(RecyclerView recyclerView, int position) {

                                }

                                @Override
                                public void OnRecyclerViewItemBind(RecyclerView recyclerView, GenericAdapter.GenericHolder view, int position) {
                                    try {
                                        JSONObject currGuestNameList = filteredGuestRoomList.getJSONObject(position);
                                        view.txtArticlesName.setText(currGuestNameList.getString("name"));
                                        view.txtArticlesName.setTextSize(15);
                                        view.txtArticlesName.setTextColor(Color.BLUE);

                                        view.txtArticlesPrice.setText(currGuestNameList.getString("zinr"));
                                        view.txtArticlesPrice.setTextSize(15);
                                        view.txtArticlesPrice.setTextColor(Color.BLUE);

                                        view.itemView.setBackgroundResource(R.color.colorWhite);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }

                                }

                                @Override
                                public int OnRecyclerViewItemCount(RecyclerView recyclerView) {
                                    return filteredGuestRoomList.length();
                                }
                            });

                            dialogBuilderRoomList.setNegativeButton("", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                }
                            });


                            AlertDialog alertDialog = dialogBuilderRoomList.create();
                            alertDialog.show();


                            return true;
                        }
                    }
                    return false;
                }
            });


    checkingOpenTable();

    try {
        dialogBuilder.setTitle("Table " + currTable.getString("tischnr"));
    } catch (JSONException e) {
        e.printStackTrace();
    }
    dialogBuilder.setPositiveButton("Confirm Table", new DialogInterface.OnClickListener() {


        public void onClick(DialogInterface dialog, int whichButton) {

        }
    });

    dialogBuilder.setNegativeButton("", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
        }
    });
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();
}

我尝试在公共类中声明RecyclerView但我仍然得到相同的错误。任何答案都会对我有帮助。

之前谢谢

1 个答案:

答案 0 :(得分:1)

您的RecyclerView位于由R.layout.guestname_list_activity dialogView充气的视图内,因此您的初始化应为

final RecyclerView recyclerViewGuestNameList = (RecyclerView) dialogView.findViewById(R.id.recyclerViewGuestNameList);

据说对话框中的对话框并不是最好的方法。