Search code examples
androidexceptionsplash-screen

Trying to use splash screen in my android project gives exception


I am trying to invoke a start activity or splash screen for some time (5 seconds) in my application but I am getting exception.I just want this activity to load for 5 seconds and then vanish and then main activity comes. I am new to android.Any help would be appreciated:

MY code is like this:

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)

Solution

  • Use StartActivity.this instead of getApplicationContext() for creating ProgressDialog object like:

    ProgressDialog pbar=new ProgressDialog(StartActivity.this);