尝试在我的 android 项目中使用启动画面时出现异常
Trying to use splash screen in my android project gives exception
我正在尝试在我的应用程序中调用启动 activity 或启动画面一段时间(5 秒),但我得到 exception.I 只想加载此 activity 5 秒后消失,然后 main activity 出现。我是 android.Any 的新手,将不胜感激:
我的代码是这样的:
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.access0(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.access0(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.access0(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.access0(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()
来创建 ProgressDialog
对象,例如:
ProgressDialog pbar=new ProgressDialog(StartActivity.this);
每当您在移动到其他屏幕或屏幕旋转之前显示进度对话框时,您必须关闭进度对话框。
ProgressDialog pbar=new ProgressDialog(StartActivity.this);
.....
pbar.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
pbar.dismiss();
...
}
}, 5000);
将getApplicationContext()
更改为StartActivity.this
同时添加 Progress
关闭代码,否则你会得到 android.view.WindowLeaked
错误
将您的代码更改为以下...
final ProgressDialog pbar = new ProgressDialog(StartActivity.this);
pbar.setMessage("Connecting...");
pbar.setCancelable(false);
pbar.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (pbar != null && pbar.isShowing()) {
pbar.dismiss();
}
Intent mainIntent = new Intent(StartActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
}
}, 5000);
替换此行
ProgressDialog pbar=new ProgressDialog(StartActivity.this);
和
ProgressDialog pbar=new ProgressDialog(getApplicationContext());
我正在尝试在我的应用程序中调用启动 activity 或启动画面一段时间(5 秒),但我得到 exception.I 只想加载此 activity 5 秒后消失,然后 main activity 出现。我是 android.Any 的新手,将不胜感激:
我的代码是这样的:
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.access0(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.access0(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.access0(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.access0(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()
来创建 ProgressDialog
对象,例如:
ProgressDialog pbar=new ProgressDialog(StartActivity.this);
每当您在移动到其他屏幕或屏幕旋转之前显示进度对话框时,您必须关闭进度对话框。
ProgressDialog pbar=new ProgressDialog(StartActivity.this);
.....
pbar.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
pbar.dismiss();
...
}
}, 5000);
将getApplicationContext()
更改为StartActivity.this
同时添加 Progress
关闭代码,否则你会得到 android.view.WindowLeaked
错误
将您的代码更改为以下...
final ProgressDialog pbar = new ProgressDialog(StartActivity.this);
pbar.setMessage("Connecting...");
pbar.setCancelable(false);
pbar.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (pbar != null && pbar.isShowing()) {
pbar.dismiss();
}
Intent mainIntent = new Intent(StartActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
}
}, 5000);
替换此行
ProgressDialog pbar=new ProgressDialog(StartActivity.this);
和
ProgressDialog pbar=new ProgressDialog(getApplicationContext());