2012-03-12 27 views
0

起初我的啓動畫面工作得很好,但後來我試圖加入一個能夠破壞活動飛濺的代碼。我通過將onPause方法放入protected void的末尾來完成此操作。初始屏幕不會失敗? Java

這是投入的方法

'package com.shipment.emulatorfix; 

'import android.app.Activity; 
'import android.content.Intent; 
'import android.media.MediaPlayer; 
'import android.os.Bundle; 

'public class Splash extends Activity{ 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 
    Thread timer = new Thread(){ 
     public void run(){ 
      try{ 
       sleep(5000); 
      } catch(InterruptedException e){ 
       e.printStackTrace(); 
      }finally{ 
       Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY"); 
       startActivity(openMain); 
      } 
     } 
    }; 
timer.start(); 
} 



'} 

之前啓動畫面是這樣的代碼

'package com.shipment.emulatorfix; 

'import android.app.Activity; 
'import android.content.Intent; 
'import android.media.MediaPlayer; 
'import android.os.Bundle; 

'public class Splash extends Activity{ 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 
    Thread timer = new Thread(){ 
     public void run(){ 
      try{ 
       sleep(5000); 
      } catch(InterruptedException e){ 
       e.printStackTrace(); 
      }finally{ 
       Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY"); 
       startActivity(openMain); 
      } 
     } 
    }; 
timer.start(); 
} 

@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    finish(); 
} 

'} 

任何幫助後,將不勝感激,謝謝。

+0

put finish();在finally塊中。 – Nishant 2012-03-12 03:56:07

回答

2

試試這個

public class SplashScreen extends Activity { 

protected int _splashTime = 2000; 
private Thread splashTread; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash);   

    splashTread = new Thread() { 
     @Override 
     public void run() { 
      try {     
       synchronized(this) { 
        wait(_splashTime); 
       }     
      } catch(InterruptedException e) { 
       System.out.println("EXc=" + e); 
      } 
      finally {    

       startActivity(new Intent(SplashScreen.this, Login.class));     
       //stop(); 
       finish(); 
      } 
     } 
    };  
    splashTread.start(); 
    } 
} 
+0

對不起,但沒有做任何事情。它也沒有啓動與代碼 – user1260584 2012-03-12 04:12:06

+0

此代碼完全爲我工作的飛濺 – 2012-03-12 04:12:59

0
public class Welcome extends Activity 
{ 
/** Called when the activity is first created. */ 
    Handler mHandler,actHandler;   

    @Override 
     public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.welcome); 

    new Thread(){ 
     public void run(){ 
     try{     
      Thread.sleep(3000);     
      }        
     catch(Exception ex){ 

      Log.e("Welcome Exception :",ex.toString()); 
      } 
       try{ 
       Message msg=mHandler.obtainMessage(); 
       mHandler.sendMessage(msg);  
       } 
       catch(NullPointerException ex){ 
       Log.e("Handler Exception :",ex.toString());               
       }      
       } 

    }.start(); 
     mHandler=new Handler(){ 
     public void handleMessage(Message msg){ 
     super.handleMessage(msg);     


     Intent i=new Intent(Welcome.this,M_chat.class); 
     startActivity(i); 
     finish(); 
     } 
     }; 
     } 
    } 
0

完成的活動,然後開始其他活動。

Thread timer = new Thread(){ 
    public void run(){ 
     try{ 
      sleep(5000); 
     } catch(InterruptedException e){ 
      e.printStackTrace(); 
     }finally{ 
      finish(); 
      Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY"); 
      startActivity(openMain); 
     } 
    } 
}; 
0

這應該適合你。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 
    Thread timer = new Thread() { 
     public void run() { 
      try { 
       sleep(5000); 
      } catch(InterruptedException e){ 
       e.printStackTrace(); 
      } finally { 
       Intent openMain = new Intent("android.intent.action.TESTINGEMULATORACTIVITY"); 
       startActivity(openMain); 
       Splash.this.finish(); 
      } 
     } 
    }; 
    timer.start(); 
}