2013-03-26 53 views
0

我正在爲我的對話框使用自定義佈局。這是util java中的一種方法(不是活動)。顯示對話框中的例外

public void showLoadingProgress(String msg){ 
    Log.d("EditorUtil", "show loading progress"+progressDialog);//No i18n 
    if(progressDialog!=null && progressDialog.isShowing()){ 
     TextView message = (TextView)progressDialog.findViewById(R.id.progressmsg); 
     message.setText(msg); 
     return; 
    } 

Context context = EditorActivity.getActivity().getApplicationContext(); 
progressDialog = new Dialog(EditorActivity.getActivity().getApplicationContext()); 
progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
progressDialog.setCancelable(false); 
progressDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); 
progressDialog.setContentView(R.layout.customprogress); 

TextView message = (TextView)progressDialog.findViewById(R.id.progressmsg); 
message.setText(msg); 

progressDialog.show(); 
Log.d("",progressDialog.isShowing()+""); //No i18n 

}

我得到了一個異常喜歡

03-26 11:23:41.672: W/dalvikvm(8034): threadid=1: thread exiting with uncaught exception (group=0x40c2d930) 
03-26 11:23:41.732: E/AndroidRuntime(8034): FATAL EXCEPTION: main 
03-26 11:23:41.732: E/AndroidRuntime(8034): java.lang.IllegalStateException: Could not execute method of the activity 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at android.view.View$1.onClick(View.java:3597) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at android.view.View.performClick(View.java:4202) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at android.view.View$PerformClick.run(View.java:17340) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at android.os.Handler.handleCallback(Handler.java:725) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at android.os.Handler.dispatchMessage(Handler.java:92) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at android.os.Looper.loop(Looper.java:137) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at android.app.ActivityThread.main(ActivityThread.java:5039) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at java.lang.reflect.Method.invoke(Method.java:511) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at dalvik.system.NativeStart.main(Native Method) 
03-26 11:23:41.732: E/AndroidRuntime(8034): Caused by: java.lang.reflect.InvocationTargetException 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at java.lang.reflect.Method.invoke(Method.java:511) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at android.view.View$1.onClick(View.java:3592) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  ... 11 more 
03-26 11:23:41.732: E/AndroidRuntime(8034): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at android.view.ViewRootImpl.setView(ViewRootImpl.java:571) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 
03-26 11:23:41.732: E/AndroidRuntime(8034):  at android.app.Dialog.show(Dialog.java:281 
+0

具有u試圖'的EditorActivity.getActivity()''代替EditorActivity.getActivity() .getApplicationContext()' –

回答

1

用於顯示progressDialog非活動調用後,你將需要通過當前活動上下文,而不是應用程序上下文到對話框構造函數。可以通過添加一個參數傳遞活動上下文showLoadingProgress:

public void showLoadingProgress(String msg,Context context){ 

//..... 
progressDialog = new Dialog(context); 
//..... 

} 

現在通過作爲EditorActivity.getActivity()第二個參數showLoadingProgress方法