The following code is for the splash screen in my application. What I need to do is when the activity loads, the image should be displayed by default, and then after a few seconds the image should be changed to another in the same action. I have a different image, similar to the first, with a different color. I wanted to change the screen for this image in a few seconds.
I did it as in my code.
package com.ruchira.busguru; import android.media.MediaPlayer; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.RelativeLayout; public class SplashScreen extends Activity { ImageView imgBus; MediaPlayer introSound, bellSound; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash_screen); imgBus = (ImageView) findViewById(R.id.imgBus); imgBus.setImageResource(R.drawable.blackbus); introSound = MediaPlayer.create(SplashScreen.this, R.raw.enginestart); introSound.start(); Thread timer = new Thread(){ public void run(){ try{ sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); }finally{ imgBus.setImageResource(R.drawable.bluekbus); } } }; timer.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.splash_screen, menu); return true; } @Override protected void onPause() {
but the problem is that the program stops executing in a thread saying ...
"android.view.ViewRoot $ CalledFromWrongThreadException: only the source thread that created the view hierarchy can touch its views."
How can I achieve this? Someone please help me deal with this issue. I am new to Android development.
Thanks.
source share