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
4

1 回答 1

1

为了从非活动调用中显示progressDialog,您需要将当前活动上下文而不是应用程序上下文传递给对话框构造函数。您可以通过再添加一个参数将 Activity 上下文传递给 showLoadingProgress:

public void showLoadingProgress(String msg,Context context){

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

}

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

于 2013-03-26T06:03:20.443 回答