2012-03-13 51 views
0

我製作了一個與SQLite數據庫通信的程序。現在,我試圖添加一個窗口來確認用戶是否真的想清理數據庫,但我有一些錯誤。這是我的第一個窗口提醒dialogo。AlertDialog不打開

cleanbtm.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); 
      builder.setMessage(R.string.confirm_clean_table) 
      .setCancelable(false) 
      .setPositiveButton(R.string.afirmative_text, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dbAdapter dbAdapter = new dbAdapter(getApplicationContext()); 
        dbAdapter.eraseGoods(); 
       } 
      }) 
      .setNegativeButton(R.string.negative_text, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 

     } 
    }); 

的logcat的顯示下一個錯誤:

03-13 13:12:49.445: W/KeyCharacterMap(224): No keyboard for id 0 
03-13 13:12:49.452: W/KeyCharacterMap(224): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
03-13 13:13:03.212: I/dalvikvm(224): threadid=7: reacting to signal 3 
03-13 13:13:03.232: I/dalvikvm(224): Wrote stack trace to '/data/anr/traces.txt' 
03-13 13:13:04.522: D/dalvikvm(224): GC freed 3476 objects/231880 bytes in 86ms 
03-13 13:13:14.432: I/global(224): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required. 
03-13 13:13:18.524: W/KeyCharacterMap(224): No keyboard for id 0 
03-13 13:13:18.524: W/KeyCharacterMap(224): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
03-13 13:13:23.002: D/AndroidRuntime(224): Shutting down VM 
03-13 13:13:23.002: W/dalvikvm(224): threadid=3: thread exiting with uncaught exception (group=0x4001aa28) 
03-13 13:13:23.002: E/AndroidRuntime(224): Uncaught handler: thread main exiting due to uncaught exception 
03-13 13:13:23.024: E/AndroidRuntime(224): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.ViewRoot.setView(ViewRoot.java:460) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.app.Dialog.show(Dialog.java:238) 
03-13 13:13:23.024: E/AndroidRuntime(224): at com.remotedata.firstapp.chartView$2.onClick(chartView.java:54) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.View.performClick(View.java:2344) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.View.onTouchEvent(View.java:4133) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.widget.TextView.onTouchEvent(TextView.java:6510) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.View.dispatchTouchEvent(View.java:3672) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) 
03-13 13:13:23.024: E/AndroidRuntime(224): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712) 
03-13 13:13:23.024: E/AndroidRuntime(224): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.app.Activity.dispatchTouchEvent(Activity.java:1987) 
03-13 13:13:23.024: E/AndroidRuntime(224): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.view.ViewRoot.handleMessage(ViewRoot.java:1658) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.os.Looper.loop(Looper.java:123) 
03-13 13:13:23.024: E/AndroidRuntime(224): at android.app.ActivityThread.main(ActivityThread.java:4203) 
03-13 13:13:23.024: E/AndroidRuntime(224): at java.lang.reflect.Method.invokeNative(Native Method) 
03-13 13:13:23.024: E/AndroidRuntime(224): at java.lang.reflect.Method.invoke(Method.java:521) 
03-13 13:13:23.024: E/AndroidRuntime(224): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
03-13 13:13:23.024: E/AndroidRuntime(224): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 
03-13 13:13:23.024: E/AndroidRuntime(224): at dalvik.system.NativeStart.main(Native Method) 
03-13 13:13:23.032: I/dalvikvm(224): threadid=7: reacting to signal 3 
03-13 13:13:23.041: I/dalvikvm(224): Wrote stack trace to '/data/anr/traces.txt' 
+0

嘗試改變getApplicationContext()來YourActivityName.this – FoamyGuy 2012-03-13 13:25:27

+0

嘗試通過你的活動納入'AlertDialog.builder'而不是應用程序上下文。 – 2012-03-13 13:25:38

+0

謝謝蒂姆你是對的,這是解決方案! – 2012-03-13 13:30:00

回答

2

您需要提供活動爲背景....

AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this); 
+0

我錯了你是對的 – 2012-03-13 13:29:16

0

你應該寫

@Override 
protected Dialog onCreateDialog(int id){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage(R.string.confirm_clean_table) 
      ........ 
      ........ 
     return builder.create(); 
    } 

和onClick()

public void onClick(View v) { 
    showDialog(Your Id); 
} 
0

爲了創建一個對話,你可以使用下面的代碼:

new AlertDialog.Builder(this) 
    .setTitle("") 
    .setCancelable(false) 
    .setMessage(getResources().getString(R.string.hintformultiplechoice)) 
    .setPositiveButton(R.string.afirmative_text, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dbAdapter dbAdapter = new dbAdapter(getApplicationContext()); 
       dbAdapter.eraseGoods(); 
      } 
     }) 
    .setNegativeButton(R.string.negative_text, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
     }) 

    .show(); 
相關問題