尝试在我的android项目中使用闪屏会出现异常

尝试在我的android项目中使用闪屏会出现异常,android,exception,splash-screen,Android,Exception,Splash Screen,我试图在我的应用程序中调用启动活动或启动屏幕一段时间(5秒),但遇到异常。我只希望此活动加载5秒,然后消失,然后主活动出现。我是android新手。如有任何帮助,将不胜感激: 我的代码如下: public class StartActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceS

我试图在我的应用程序中调用启动活动或启动屏幕一段时间(5秒),但遇到异常。我只希望此活动加载5秒,然后消失,然后主活动出现。我是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。此
代替
getApplicationContext()
来创建
ProgressDialog
对象,如:

ProgressDialog pbar=new ProgressDialog(StartActivity.this);

使用
StartActivity。此
代替
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);

当您在移动到其他屏幕或屏幕旋转之前显示进度对话框时,必须关闭进度对话框

ProgressDialog pbar=new ProgressDialog(StartActivity.this);
        .....
        pbar.show();

       new Handler().postDelayed(new Runnable() {
               @Override
               public void run() {
                    pbar.dismiss();
                    ...

                   }
               }, 5000);

getApplicationContext()
更改为
StartActivity。此

同时添加
Progress
discouse代码,否则您将得到
android.view.windowsleed
错误

将代码更改为下面的

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);

getApplicationContext()
更改为
StartActivity。此

同时添加
Progress
discouse代码,否则您将得到
android.view.windowsleed
错误

将代码更改为下面的

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(StartActivity.this);


使用
StartActivity。这是
而不是
getApplicationContext()
,并且使用
pbar.disease()是关闭进度对话框使用
StartActivity。这是
而不是
getApplicationContext()
并且是使用
pbar.disease()关闭进度对话框但是这样使用它的原因是什么。@GonchuGolu,ProgressDialog需要当前活动上下文引用而不是应用程序上下文引用。但是这样使用它的原因是什么。@GonchuGolu,ProgressDialog需要当前活动上下文引用而不是应用程序上下文引用。