5

I want to create a simple ProgressDialog in my Activity. I created it like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.channellist);

    final Context c=this;

    t1=(TextView) findViewById(R.id.channellistStatus);

    listView=(ListView) findViewById(R.id.channellist);

    rc=RCManager.getInstance();

    chlistAdapter = new ChannelListAdapter(this,R.layout.channellist_row_lyt,clist);

    listView.setAdapter(chlistAdapter);

    t1.setText("No Channel List Found...");

    progDailog=ProgressDialog.show(this,"HI", "Loading");

    new Thread(new Runnable() {

        @Override
        public void run() {
            try 
            {
                Thread.sleep(3000);
            } 
            catch (InterruptedException e) 
            {
                e.printStackTrace();
            }   
            updateChannelList();
        }
    }).start();

But it is resulting in the following Exception ie WindowManager BadToken Exception. I don't know why it is. Please can anyone help me to solve this?

05-23 16:56:44.573: ERROR/AndroidRuntime(2494): FATAL EXCEPTION: main
05-23 16:56:44.573: ERROR/AndroidRuntime(2494): java.lang.RuntimeException: Unable            to start activity           ComponentInfo{com.nds.fr.activities/com.nds.fr.activities.TabGroup2Activity}:  java.lang.RuntimeException: Unable to start activity   ComponentInfo{com.nds.fr.activities/com.nds.fr.activities.ChannelListing}:  android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@48189240 is not valid; is your activity running?
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.widget.TabHost.setCurrentTab(TabHost.java:323)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.view.View.performClick(View.java:2408)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.view.View$PerformClick.run(View.java:8818)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.os.Handler.handleCallback(Handler.java:587)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.os.Looper.loop(Looper.java:123)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at java.lang.reflect.Method.invokeNative(Native Method)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at java.lang.reflect.Method.invoke(Method.java:521)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at dalvik.system.NativeStart.main(Native Method)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nds.fr.activities/com.nds.fr.activities.ChannelListing}: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@48189240 is not valid; is your activity running?
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at com.nds.fr.activities.TabGroupActivity.startChildActivity(TabGroupActivity.java:65)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at com.nds.fr.activities.TabGroup2Activity.onCreate(TabGroup2Activity.java:10)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     ... 18 more
05-23 16:56:44.573: ERROR/AndroidRuntime(2494): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@48189240 is not valid; is your activity running?
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.view.ViewRoot.setView(ViewRoot.java:505)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:200)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.Dialog.show(Dialog.java:241)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.ProgressDialog.show(ProgressDialog.java:107)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.ProgressDialog.show(ProgressDialog.java:90)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.ProgressDialog.show(ProgressDialog.java:85)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at com.nds.fr.activities.ChannelListing.onCreate(ChannelListing.java:65)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-23 16:56:44.573: ERROR/AndroidRuntime(2494):     ... 25 more
forsvarir
  • 10,749
  • 6
  • 46
  • 77
bHaRaTh
  • 3,464
  • 4
  • 34
  • 32

3 Answers3

19
  progDailog=ProgressDialog.show(getParent(),"HI", "Loading");
Tanmay Mandal
  • 39,873
  • 12
  • 51
  • 48
1

You can't show the dialog before the activity became visible to the user. Call the showing in onResume() or in some button onClickListener.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
0

If you create a new Thread and set it to sleep for some time, only the new Thread sleeps. That has no effect on the UI-Thread where you'r Activity is created! Also, this isn't a good way of doing this anyways.

Also, you can't show the Dialog before your Activity became visible. Try it in the onStart-method.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111