2013-09-25 123 views
0

我厭倦了在自定義列表視圖中顯示自定義對話框。懇求幫助顯示錯誤對話框

代碼:

more.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Dialog dialog = new Dialog(mContext.getApplicationContext()); 
       dialog.setTitle("Discription"); 
       dialog.setContentView(R.layout.dialog_item_simple_text); 
       TextView textViewItem = (TextView) dialog 
         .findViewById(R.id.textViewItem); 
       textViewItem.setText(data.short_description); 
       dialog.setCancelable(true); 
       dialog.setCanceledOnTouchOutside(true); 
       dialog.show();//<--Error at this point 
      } 
     }); 

有錯誤,如: logcat的按摩

FATAL EXCEPTION: main android.view.WindowManager$BadTokenException: Unable to add 
window -- token null is not for an application 
at android.view.ViewRootImpl.setView(ViewRootImpl.java:563) 
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269) 
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 
at android.app.Dialog.show(Dialog.java:281) 
at com.fashionbargains.adapter.DnSAdapter$1.onClick(DnSAdapter.java:88) 
at android.view.View.performClick(View.java:4240) 
at android.view.View$PerformClick.run(View.java:17721) 
at android.os.Handler.handleCallback(Handler.java:730) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5103) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 

我有一個是通過BaseTask類是擴展AsyncTask

回答

0

稱爲自定義適配器類你需要參考當前的活動,如...

替換此:

Dialog dialog = new Dialog(mContext.getApplicationContext()); 

與此:

Dialog dialog = new Dialog(YourClassname.this); 

編輯:

如果您的onClick是在你需要使用runOnUiThreadAsyncTask Class

runOnUiThread(new Runnable() { 

        @Override 
       public void run() { 
        Dialog dialog = new Dialog(mContext.getApplicationContext()); 
        dialog.setTitle("Discription"); 
        dialog.setContentView(R.layout.dialog_item_simple_text); 
        TextView textViewItem = (TextView) dialog 
          .findViewById(R.id.textViewItem); 
        textViewItem.setText(data.short_description); 
        dialog.setCancelable(true); 
        dialog.setCanceledOnTouchOutside(true); 
        dialog.show();//<--Error at this point 

    } 
}); 
+0

我試過了 –

+0

@Survivor你試過runOnUiThread嗎? –

+0

我有擴展AsyncTask的基類,在doInBackground方法中,我打電話給我的自定義適配器類,並在公共視圖getView方法 –