所以我開始爲一個簡單的Android遊戲製作一個簡單的「啓動畫面」(它更像是一個介紹畫面),我正在爲一個移動編程課程做一個簡單的安卓遊戲。我遇到了問題。我知道我應該使用一個線程,但我的實現似乎不工作。你應該能夠感受到我要去的效果。Splash Screen&Premise Screen - Android
public class SplashScreen extends Activity {
//how long until we go to the next activity
protected int splashTime = 2000;
private Thread splashThread;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
final TextView first = (TextView) findViewById(R.id.textView1);
final TextView second = (TextView) findViewById(R.id.textView2);
final TextView third = (TextView) findViewById(R.id.textView3);
final TextView fourth = (TextView) findViewById(R.id.textView4);
final TextView fifth = (TextView) findViewById(R.id.textView5);
final TextView sixth = (TextView) findViewById(R.id.textView6);
final ImageView main_character = (ImageView) findViewById(R.id.imageView1);
// thread for displaying the SplashScreen
splashThread = new Thread() {
@Override
public void run() {
try {
synchronized(this){
first.setText("The year is 2048...");
wait(splashTime);
first.setText("");
second.setText("...the Earth's resources have long been depleted");
wait(splashTime);
second.setText("");
third.setText("Attempts have been made to save our home...");
wait(splashTime);
third.setText("");
fourth.setText("...but all has gone awry, trash now rains from the skies");
wait(splashTime);
fourth.setText("");
fifth.setText("Now, only one man can save us, and his name is...");
wait(splashTime);
fifth.setText("");
sixth.setText("The Garbage Man!");
main_character.setImageResource(drawable.bobrgb888);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
finally {
finish();
//start a new activity
Intent i = new Intent(getApplicationContext(), MainMenu.class);
startActivity(i);
}
}
};
splashThread.start();
}
問題是,第一個textview與第一個wait()調用一起被調用,但隨後我得到一個強制關閉。
這裏是我的logcat:
07-02 19:35:16.241: W/dalvikvm(709): threadid=9: thread exiting with uncaught exception (group=0x40015560)
07-02 19:35:16.250: E/AndroidRuntime(709): FATAL EXCEPTION: Thread-10
07-02 19:35:16.250: E/AndroidRuntime(709): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-02 19:35:16.250: E/AndroidRuntime(709): at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
07-02 19:35:16.250: E/AndroidRuntime(709): at android.view.ViewRoot.requestLayout(ViewRoot.java:629)
07-02 19:35:16.250: E/AndroidRuntime(709): at android.view.View.requestLayout(View.java:8267)
07-02 19:35:16.250: E/AndroidRuntime(709): at android.view.View.requestLayout(View.java:8267)
07-02 19:35:16.250: E/AndroidRuntime(709): at android.view.View.requestLayout(View.java:8267)
07-02 19:35:16.250: E/AndroidRuntime(709): at android.view.View.requestLayout(View.java:8267)
07-02 19:35:16.250: E/AndroidRuntime(709): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
07-02 19:35:16.250: E/AndroidRuntime(709): at android.view.View.requestLayout(View.java:8267)
07-02 19:35:16.250: E/AndroidRuntime(709): at android.widget.TextView.checkForRelayout(TextView.java:5521)
07-02 19:35:16.250: E/AndroidRuntime(709): at android.widget.TextView.setText(TextView.java:2724)
07-02 19:35:16.250: E/AndroidRuntime(709): at android.widget.TextView.setText(TextView.java:2592)
07-02 19:35:16.250: E/AndroidRuntime(709): at android.widget.TextView.setText(TextView.java:2567)
07-02 19:35:16.250: E/AndroidRuntime(709): at com.connor.black.SplashScreen$1.run(SplashScreen.java:41)
07-02 19:35:16.420: D/szipinf(709): Initializing inflate state
如果你在後臺線程而不是UI線程上這樣做,它不會有你正在尋找的效果。如果您不介意在介紹發生時用戶無法做任何事情,則可能不希望在另一個線程上執行此操作。或者你也可以使用AsyncTask,雖然這通常更像是在後臺做一些東西時顯示進度條。 – matt5784