显示对话框时出现异常

时间:2013-03-26 05:55:23

标签: android android-widget

我在对话框中使用自定义布局。这是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

1 个答案:

答案 0 :(得分:1)

要显示来自非Activity调用的progressDialog,您需要将当前活动上下文而不是Application Context传递给Dialog Constructor。您可以通过添加一个参数来将Activity上下文传递给showLoadingProgress:

public void showLoadingProgress(String msg,Context context){

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

}

现在将EditorActivity.getActivity()作为第二个参数传递给showLoadingProgress方法