创建AlertDialog时出错

时间:2013-08-11 16:29:51

标签: android android-alertdialog

我想在点击按钮时显示AlertDialog,这是我的代码:

        @Override
        public void onClick(View arg0) {
            Log.d("","Kliknieto");
            AlertDialog.Builder b = new AlertDialog.Builder(ctx);
            b.setTitle(R.string.camerae);

            if(dpm.getCameraDisabled(c))
            {
            b.setMessage(R.string.enabled);
            b.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()  {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();


                }
            });
            b.setPositiveButton(R.string.senable, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    //Some code removed

                }
            });
            }

            else
            {
            b.setMessage(R.string.disabled);
            b.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()  {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();


                }
            });
            b.setPositiveButton(R.string.sdisable, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    //Some code removed

                }
            });
            }

            b.show();

        } 

每个变量都正确减速。我不知道我的错误是什么原因。 LogCat说:

08-11 16:24:39.708: E/AndroidRuntime(951): FATAL EXCEPTION: main
08-11 16:24:39.708: E/AndroidRuntime(951): java.lang.NullPointerException
08-11 16:24:39.708: E/AndroidRuntime(951):  at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
08-11 16:24:39.708: E/AndroidRuntime(951):  at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
08-11 16:24:39.708: E/AndroidRuntime(951):  at com.radzik.devadmin.MainActivity$5.onClick(MainActivity.java:140)
08-11 16:24:39.708: E/AndroidRuntime(951):  at android.view.View.performClick(View.java:4084)
08-11 16:24:39.708: E/AndroidRuntime(951):  at android.view.View$PerformClick.run(View.java:16966)
08-11 16:24:39.708: E/AndroidRuntime(951):  at android.os.Handler.handleCallback(Handler.java:615)
08-11 16:24:39.708: E/AndroidRuntime(951):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-11 16:24:39.708: E/AndroidRuntime(951):  at android.os.Looper.loop(Looper.java:137)
08-11 16:24:39.708: E/AndroidRuntime(951):  at android.app.ActivityThread.main(ActivityThread.java:4745)
08-11 16:24:39.708: E/AndroidRuntime(951):  at java.lang.reflect.Method.invokeNative(Native Method)
08-11 16:24:39.708: E/AndroidRuntime(951):  at java.lang.reflect.Method.invoke(Method.java:511)
08-11 16:24:39.708: E/AndroidRuntime(951):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-11 16:24:39.708: E/AndroidRuntime(951):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-11 16:24:39.708: E/AndroidRuntime(951):  at dalvik.system.NativeStart.main(Native Method)

@ADDITIONAL INFO

第140行是:

AlertDialog.Builder b = new AlertDialog.Builder(ctx);

2 个答案:

答案 0 :(得分:1)

您的上下文等于null。我建议你使用,

AlertDialog.Builder b = new AlertDialog.Builder(your_Activity.this);

而不是

AlertDialog.Builder b = new AlertDialog.Builder(ctx);

答案 1 :(得分:1)

试试这个:

AlertDialog.Builder b = new AlertDialog.Builder(NameOfYourActivity.this);