2013-11-09 38 views
0

我正在嘗試創建一個線程,該線程是在我的應用程序的其餘部分正在加載時出現的初始屏幕,但由於某種原因,我的初始活動在2秒後不會消失,因爲它應該如此。這是爲什麼?初始屏幕不會消失

這裏是我的飛濺活動類:

imports ... 

public class Splash extends Activity implements Runnable { 

    @Override 
    protected void onCreate(Bundle tokenArg) { 
     super.onCreate(tokenArg); 
     setContentView(R.layout.splash); 

     Thread splashing = new Thread(); 
     splashing.start(); 

    } 

    @Override 
    public void run() { 
     try { 
      Thread.sleep(2000); 
      startActivity(new Intent(Splash.this, Home.class)); 
     } 
     catch(Exception excpt) { 
      AlertDialog alert = new AlertDialog.Builder(this).create(); 
      alert.setTitle("Error"); 
      alert.setMessage("App is going to close"); 
     } 
     finally { 
      this.finish(); 
     } 
    } 
} 

這是。家裏活動類:

public class Home extends Activity { 

    @Override 
    protected void onCreate(Bundle tokenArg) { 
     super.onCreate(tokenArg); 
     setContentView(R.layout.activity_home); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.home, menu); 
     return true; 
    } 
} 

兩者都有其相應的XML和它是所有好與他們。 (我已經單獨測試過)

在此先感謝您的時間。

回答

1

線程的示例使用此代碼啓動您的活動。

new Handler().postDelayed(new Runnable(){ 
       public void run() { 
        Intent mainIntent = new Intent(Splash.this, Home.class) 
          .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) 
          .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
        Splash.this.startActivity(mainIntent); 

        Splash.this.finish(); 
       } 
      }, 2000); 
0

試試看看這個代碼。它處理了很多用例,並且很好地處理用戶的後退按鈕和其他很多用例。

public class SplashBranding extends Activity { 

    private Handler mHandler; 
    private static final long TWO_SECOND_IN_MS = 2000; 

    @Override 
    protected void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 
     mHandler = new ShowHomeHandler(); 
     setContentView(R.layout.splash); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     mHandler.sendEmptyMessageDelayed(0, TWO_SECOND_IN_MS); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     // In case system dialogs appear and this method is called, we shouldn't show Home. 
     if(!isFinishing()) 
      mHandler.removeMessages(0); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     // In case a call is received and this method is called, we shouldn't show Home. 
     if(!isFinishing()) 
      mHandler.removeMessages(0); 
    } 

    @Override 
    public void onBackPressed() { 
     super.onBackPressed(); 
     // In case user pressed back, we shouldn't show Home. 
     mHandler.removeMessages(0); 
    } 

    /** 
    * Shows the Home Activity. 
    */ 
    private void showHome() { 
     Intent i = new Intent(this, Home.class); 
     startActivity(i); 
     finish(); 
    } 

    private class ShowHomeHandler extends Handler { 

     @Override 
     public void handleMessage(Message msg) { 
      super.handleMessage(msg); 

      switch (msg.what) { 
      case 0: 
       showHome(); 
       break; 
      default: 
       break; 
      } 
     } 
    } 

} 
2

在實例化線程時,您沒有將可運行參數作爲參數傳遞給線程構造函數。正如您已經在Splash.class中實現了Runnable接口,將當前對象作爲參數傳遞。

Thread splashing = new Thread(this); 
splashing.start(); 

希望這對我有幫助。

0

看起來你沒有通過new Thread();的課程。您應該嘗試將其更改爲new Thread(this);

0

你可以試試這個簡單的代碼 ..

@Override 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    *// activity_splay is the name of splash .xml file* 
    setContentView(R.layout.activity_splash); 
    Thread thread = new Thread(){ 
     @Override 
     public void run(){ 
      try{ 
       // splash screen would disappear after 1500 ms i.e 1.5 sec 
       sleep(1500); <br/> 
     // MainActivity is the name of activity that would appear after splash screen 
     startActivity(new Intent(getApplicationContext(), MainActivity.class)); 
      } 
      catch (InterruptedException e){ 
       e.printStackTrace(); 
      } 
     } 
    }; 
    thread.start(); 
} 
//hopefully it would help:)