4

我已经为我的 alertdialog 做了一个布局,我有一个退出按钮可以调用dialog.dismiss(),还有一些可以将数据保存到首选项。我已将 onClickListeners 设置为 all 并且我的对话框没有任何按钮,例如正面或负面,只有布局中的按钮。

但是,调用对话框时应用程序崩溃。

08-20 07:58:01.291: E/AndroidRuntime(3297): FATAL EXCEPTION: main
08-20 07:58:01.291: E/AndroidRuntime(3297): java.lang.NullPointerException
08-20 07:58:01.291: E/AndroidRuntime(3297):   at com.xcommerce.androidstore.HomeScreen$21.onClick(HomeScreen.java:1183)
08-20 07:58:01.291: E/AndroidRuntime(3297):   at android.view.View.performClick(View.java:4202)
08-20 07:58:01.291: E/AndroidRuntime(3297):   at android.view.View$PerformClick.run(View.java:17340)
08-20 07:58:01.291: E/AndroidRuntime(3297):   at android.os.Handler.handleCallback(Handler.java:725)
08-20 07:58:01.291: E/AndroidRuntime(3297):   at android.os.Handler.dispatchMessage(Handler.java:92)
08-20 07:58:01.291: E/AndroidRuntime(3297):   at android.os.Looper.loop(Looper.java:137)
08-20 07:58:01.291: E/AndroidRuntime(3297):   at android.app.ActivityThread.main(ActivityThread.java:5039)
08-20 07:58:01.291: E/AndroidRuntime(3297):   at java.lang.reflect.Method.invokeNative(Native Method)
08-20 07:58:01.291: E/AndroidRuntime(3297):   at java.lang.reflect.Method.invoke(Method.java:511)
08-20 07:58:01.291: E/AndroidRuntime(3297):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-20 07:58:01.291: E/AndroidRuntime(3297):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-20 07:58:01.291: E/AndroidRuntime(3297):   at dalvik.system.NativeStart.main(Native Method)
TextView sort = (TextView) findViewById(R.id.sorteaza);
        final Dialog sortDialog = new Dialog(this, R.style.DialogTheme);
        sort.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                LayoutInflater inflater = HomeScreen.this.getLayoutInflater();
                View sortView = inflater.inflate(R.layout.sort_layout, null);
                sortDialog.setContentView(sortView);
                WindowManager manager = (WindowManager) getSystemService(Activity.WINDOW_SERVICE);
                WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                lp.copyFrom(sortDialog.getWindow().getAttributes());
                lp.width = manager.getDefaultDisplay().getWidth();
                lp.height = manager.getDefaultDisplay().getHeight() - manager.getDefaultDisplay().getHeight()/5;
                sortDialog.getWindow().setAttributes(lp);
                sortDialog.show();
                //sort controls
                SharedPreferences sortPref = getSharedPreferences("SORT", 0);
                final SharedPreferences.Editor ed = sortPref.edit();

                Button exitSort, sortAZ, sortZA, sortCheap, sortExpensive;
                exitSort = (Button) findViewById(R.id.exitSort);
                sortAZ = (Button) findViewById(R.id.sortAZ);
                sortZA = (Button) findViewById(R.id.sortZA);
                sortCheap = (Button) findViewById(R.id.sortCheap);
                sortExpensive = (Button) findViewById(R.id.sortExpensive);

                exitSort.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        sortDialog.dismiss();
                    }
                });
                sortAZ.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        ed.putString("sort", "AZ");
                        ed.commit();
                    }
                });
                sortZA.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        ed.putString("sort", "ZA");
                        ed.commit();
                    }
                });
                sortCheap.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        ed.putString("sort", "cheap");
                        ed.commit();
                    }
                });
                sortExpensive.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        ed.putString("sort", "expensive");
                        ed.commit();
                    }
                });
            }
        });
4

2 回答 2

3

您需要使用对话框对象来初始化视图。正如 Luksprog 所建议的那样。

您膨胀自定义视图并将其设置为对话框。你在你膨胀的布局中有按钮。所以使用对话框对象来初始化你的按钮。

exitSort = (Button) sortDialog.findViewById(R.id.exitSort);
sortAZ = (Button)  sortDialog.findViewById(R.id.sortAZ);
sortZA = (Button)  sortDialog.findViewById(R.id.sortZA);
sortCheap = (Button)  sortDialog.findViewById(R.id.sortCheap);
sortExpensive = (Button)  sortDialog.findViewById(R.id.sortExpensive); 
于 2013-08-20T08:19:09.660 回答
1

你的问题在线:sortDialog.dismiss();

sortDialog""一个DialogBuilder对象,它不是一个Dialog object.

show()所以你需要一个变量来处理调用函数时的对话框。前任:

final Dialog dg = sortDialog.show();

and close dialog by "dg":

dg.dismiss();
于 2013-08-20T09:58:59.930 回答