创建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();
    } 

变量声明正确,但是不知道为什么会出错:

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);

检查ctx是否为空,这里的Context需要与界面关联,需要使用XXXActivity.this,不能使用全局的Context

检查下你的ctx有值吗?