我想在應用程序中調用啓動活動或啓動畫面一段時間(5秒),但我收到異常。我只是想將此活動加載到5秒,然後消失,然後主要活動來。我是新來android.Any幫助將不勝感激:試圖在我的Android項目中使用啓動畫面給出異常
我的代碼是這樣的:
public class StartActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
ProgressDialog pbar=new ProgressDialog(getApplicationContext());
pbar.setMessage("Connecting...");
pbar.setCancelable(false);
pbar.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
final Intent mainIntent = new Intent(StartActivity.this, MainActivity.class);
StartActivity.this.startActivity(mainIntent);
StartActivity.this.finish();
}
}, 5000);
}
}
Exception I am getting is like this:
04-16 09:38:06.355 1874-1874/com.mubu.wheathertoday.wheathertoday D/﹕ HostConnection::get() New Host Connection established 0xae2f9760, tid 1874
04-16 09:38:06.371 1874-1874/com.mubu.wheathertoday.wheathertoday D/Atlas﹕ Validating map...
04-16 09:38:06.387 1874-1874/com.mubu.wheathertoday.wheathertoday D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
04-16 09:38:06.405 1874-1874/com.mubu.wheathertoday.wheathertoday E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mubu.wheathertoday.wheathertoday, PID: 1874
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mubu.wheathertoday.wheathertoday/com.mubu.wheathertoday.wheathertoday.StartActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:566)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:298)
at com.mubu.wheathertoday.wheathertoday.StartActivity.onCreate(StartActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-16 09:41:47.469 2297-2309/com.mubu.wheathertoday.wheathertoday I/art﹕ Background sticky concurrent mark sweep GC freed 1702(90KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 1367KB/1367KB, paused 1.145ms total 240.104ms
04-16 09:41:47.769 2297-2312/com.mubu.wheathertoday.wheathertoday D/OpenGLRenderer﹕ Render dirty regions requested: true
04-16 09:41:47.779 2297-2297/com.mubu.wheathertoday.wheathertoday D/﹕ HostConnection::get() New Host Connection established 0xae2ff760, tid 2297
04-16 09:41:47.789 2297-2297/com.mubu.wheathertoday.wheathertoday D/Atlas﹕ Validating map...
04-16 09:41:47.798 2297-2297/com.mubu.wheathertoday.wheathertoday D/AndroidRuntime﹕ Shutting down VM
04-16 09:41:47.811 2297-2297/com.mubu.wheathertoday.wheathertoday E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mubu.wheathertoday.wheathertoday, PID: 2297
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mubu.wheathertoday.wheathertoday/com.mubu.wheathertoday.wheathertoday.StartActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:566)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:298)
at com.mubu.wheathertoday.wheathertoday.StartActivity.onCreate(StartActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
使用'StartActivity.this',而不是'getApplicationContext()'是的解散你的進步與'pbar.dismiss()對話框;' – Piyush