vignesh
vignesh

Reputation: 1599

Exception in showing dialog

I am using custom layout for my dialog. This is a method in util java(not an activity).

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

}

I got an Exception like

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

Upvotes: 0

Views: 313

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

for showing progressDialog from non Activity call you will need to pass Current Activity Context instead of Application Context to Dialog Constructor. you can pass Activity context to showLoadingProgress by adding one more parameter:

public void showLoadingProgress(String msg,Context context){

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

}

now pass EditorActivity.getActivity() as second parameter to showLoadingProgress method

Upvotes: 1

Related Questions