程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-創建AlertDialog產生的錯誤

android-創建AlertDialog產生的錯誤

編輯:編程綜合問答
創建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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved