开发者

ProgressDialog giving error, while I am using in onCreate() of activity that is in activity group

My code is as following .

public void onCreate(Bundle savedInstanceState) {
    ................
    ................
    showDialog(CONTACT_DIALOG_ID);
            Thread t = new Thread() {
                public void run(){
                    try{

                        sleep(50000);
                    } catch(Exception e) {
                        Log.e("TAG","Exception caught in thread:" + e.toString());
                        //e.printStackTrace();
                    } finally {
                        try{
                            dismissDialog(CONTACT_DIALOG_ID);
                        } catch (IllegalArgumentException e) {
                            Log.w("TAG","Dialog does not exist");
                        }
                    }
                    //finish();  
                }
            };
            t.start();
    ................
    ................
}

and dialog is as ..

 @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;
        switch (id) {
        case CONTACT_DIALOG_ID:
            dialog = new ProgressDialog(this);
            ((ProgressDialog) dialog).setMessage("Loading, please wait...");
            break;
        }
        return dialog;
    }

Dialog is not displaying and I am getting errors as below

03-09 15:45:06.887: WARN/WindowManager(60): Attempted to add application window with unknown token android.os.BinderProxy@43c212c8.  Aborting.
03-09 15:45:06.947: DEBUG/AndroidRuntime(1119): Shutting down VM
03-09 15:45:06.957: WARN/dalvikvm(1119): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
03-09 15:45:06.957: ERROR/AndroidRuntime(1119): Uncaught handler: thread main exiting due to uncaught exception
03-09 15:45:07.087: ERROR/AndroidRuntime(1119): java.lang.IllegalStateException: Could not execute method of the activity
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.View$1.onClick(View.java:2031)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.View.performClick(View.java:2364)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.View.onTouchEvent(View.java:4179)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.View.dispatchTouchEvent(View.java:3709)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at com.android.internal.policy.开发者_如何学JAVAimpl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.os.Looper.loop(Looper.java:123)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.app.ActivityThread.main(ActivityThread.java:4363)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at java.lang.reflect.Method.invokeNative(Native Method)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at java.lang.reflect.Method.invoke(Method.java:521)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at dalvik.system.NativeStart.main(Native Method)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119): Caused by: java.lang.reflect.InvocationTargetException
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at com.SmartsourcingGlobal.Go2TagFree.TagsActivityAll.clickOnTag1(TagsActivityAll.java:109)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at java.lang.reflect.Method.invokeNative(Native Method)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at java.lang.reflect.Method.invoke(Method.java:521)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.view.View$1.onClick(View.java:2026)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     ... 40 more
03-09 15:45:07.087: ERROR/AndroidRuntime(1119): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.SmartsourcingGlobal.Go2TagFree/com.SmartsourcingGlobal.Go2TagFree.TagsActivityFiltered}: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@43d42e70 is not valid; is your activity running?
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2335)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
03-09 15:45:07.087: ERROR/AndroidRuntime(1119):     at com.SmartsourcingGlobal.Go2TagFree.TagsActivityAll.filterTag(TagsActivityAll.java:148)

What is problem in my code?


Your problem is that you try to modify the ui from non the ui-thread. Use handlers for such things. Here is a good example.


Or you can also try like below

replace dismissDialog(CONTACT_DIALOG_ID); with below code

   runOnUiThread(new Runnable()
    {            
        @Override
        public void run()
        {
            dismissDialog(CONTACT_DIALOG_ID);
        }
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜