2015-04-16 3 views
1

Я пытаюсь запустить запуск или заставку в течение некоторого времени (5 секунд) в моем приложении, но я получаю исключение. Я просто хочу, чтобы эта активность загружалась для 5 секунд, а затем исчезает, а затем начинается основная активность. Я новичок в 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.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) 
+2

Использование 'StartActivity.this' вместо' getApplicationContext() 'да и отпустить прогресс диалог с' pbar.dismiss(); ' – Piyush

ответ

2

Использование StartActivity.this вместо getApplicationContext() для создания ProgressDialog объекта как:

ProgressDialog pbar=new ProgressDialog(StartActivity.this); 
+0

но что причиной для его использования, как это , – killer

+1

@GonchuGolu, ProgressDialog требуется ссылка контекста текущей активности вместо ссылки на контекст приложения. –

+0

большое спасибо p K – killer

1

Всякий раз, когда вы показывая диалог прогресса перед тем, как вы перейдете на другой экран или на поворот экрана, вы должны отменить диалог прогресса.

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

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

        } 
       }, 5000); 
1

изменение 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); 
1

Замените эту строку

ProgressDialog pbar=new ProgressDialog(StartActivity.this); 

с

ProgressDialog pbar=new ProgressDialog(getApplicationContext()); 
Смежные вопросы