我想在我的Activity中创建一个简单的ProgressDialog
。我这样创建了它:
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();
但它导致以下异常,即 WindowManager BadToken Exception 。我不知道为什么会这样。请有人帮我解决这个问题吗?
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
答案 0 :(得分:19)
progDailog=ProgressDialog.show(getParent(),"HI", "Loading");
答案 1 :(得分:1)
在活动对用户可见之前,您无法显示对话框。调用onResume()或onClickListener上的某个按钮中的显示。
答案 2 :(得分:0)
如果您创建一个新线程并将其设置为休眠一段时间,则只有新线程会休眠。这对创建了Activity的UI-Thread没有影响!此外,这不是一个好的方法。
此外,您无法在“活动”可见之前显示“对话框”。在onStart
方法中尝试一下。