0

我想显示一个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)

@附加信息

第 140 行是:

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

2 回答 2

1

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

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

代替

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

于 2013-08-11T16:36:42.337 回答
1

试试这个:

AlertDialog.Builder b = new AlertDialog.Builder(NameOfYourActivity.this);
于 2013-08-11T16:37:44.477 回答