SachinKS 2013-08-12 03:41 采纳率: 0%
浏览 5762
已采纳

创建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);
  • 写回答

2条回答

  • kity 2013-08-12 06:00
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示