2012-08-11 136 views
1

我曾嘗試下面的代碼上點擊按鈕時,如何顯示alertdialog ..在項目列表視圖

  btnRemoveItem.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        // TODO Auto-generated method stub 
        Bundle b = new Bundle(); 
        b.putLong("index", item.id); 


        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); 
        builder.setTitle("Are you sure you ?"); 
        builder.setMessage("Are you suer you want to remove this item from the cart?"); 
        builder.setPositiveButton("No", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.cancel(); 
         } 

        }); 
        builder.setNegativeButton("Yes", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 

         } 
        }); 
        Dialog dd= builder.create(); 
        dd.show(); 

       } 
      }); 

,但我得到了下面的錯誤..

08-11 19:18:50.968: E/AndroidRuntime(827): FATAL EXCEPTION: main 
08-11 19:18:50.968: E/AndroidRuntime(827): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
08-11 19:18:50.968: E/AndroidRuntime(827): at android.view.ViewRoot.setView(ViewRoot.java:531) 
08-11 19:18:50.968: E/AndroidRuntime(827): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 
08-11 19:18:50.968: E/AndroidRuntime(827): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
08-11 19:18:50.968: E/AndroidRuntime(827): at android.app.Dialog.show(Dialog.java:241) 
08-11 19:18:50.968: E/AndroidRuntime(827): at com.gand.metro.uis.SearchActivity$ItemsAdapter$1.onClick(SearchActivity.java:406) 
08-11 19:18:50.968: E/AndroidRuntime(827): at android.view.View.performClick(View.java:2485) 
08-11 19:18:50.968: E/AndroidRuntime(827): at android.view.View$PerformClick.run(View.java:9080) 
08-11 19:18:50.968: E/AndroidRuntime(827): at android.os.Handler.handleCallback(Handler.java:587) 
08-11 19:18:50.968: E/AndroidRuntime(827): at android.os.Handler.dispatchMessage(Handler.java:92) 
08-11 19:18:50.968: E/AndroidRuntime(827): at android.os.Looper.loop(Looper.java:123) 
08-11 19:18:50.968: E/AndroidRuntime(827): at android.app.ActivityThread.main(ActivityThread.java:3683) 
08-11 19:18:50.968: E/AndroidRuntime(827): at java.lang.reflect.Method.invokeNative(Native Method) 
08-11 19:18:50.968: E/AndroidRuntime(827): at java.lang.reflect.Method.invoke(Method.java:507) 
08-11 19:18:50.968: E/AndroidRuntime(827): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
08-11 19:18:50.968: E/AndroidRuntime(827): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
08-11 19:18:50.968: E/AndroidRuntime(827): at dalvik.system.NativeStart.main(Native Method) 
+0

嘗試將getapplicationContext()更改爲[ActivityName] .this。 – nandeesh 2012-08-11 19:29:48

+0

什麼是406線? – 2012-08-11 19:29:56

回答

2

AlertDialogs只能是使用Activity上下文創建,所以getApplicationContext()將不起作用。相反,下面的全局變量添加到您的活動文件:

Context mContext; 

,然後添加以下到您的onCreate():

mContext = this; 

現在,變化:

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); 

AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 
+0

getApplicationContext()的目的是什麼以及它不起作用? – Adham 2012-08-11 19:31:05

+0

getApplicationContext()返回整個應用程序的全局上下文。 AlertDialogs只能在Activity上下文中創建,不會通過getApplicationContext()返回。 – 2012-08-11 19:34:49

0

您是usin g getApplicationContext()當您應該使用活動上下文 使用SearchActivity.this而不是