启动画面无法使用Thread

时间:2013-04-26 10:13:07

标签: android multithreading splash

我写了一个Splash Screeen,以便在应用程序启动时运行

public class SplashScreen extends Activity {

ImageView imgView;
int[] imgID = new int[]{R.drawable.frame0, R.drawable.frame1, R.drawable.frame2, R.drawable.frame3,
        R.drawable.frame4, R.drawable.frame5, R.drawable.frame6};
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    imgView = (ImageView) findViewById(R.id.imgSplash);

    new Thread(new WelcomeScreen()).start();

}

private class WelcomeScreen implements Runnable {

    @Override
    public void run() {


            try {
                for (int i = 0; i < imgID.length; i++)
                {
                    imgView.setImageResource(imgID[i]);
                    sleep(500);
                }

            } catch (InterruptedException e) {

            }finally {
                Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }


    }
}

}

收到错误“抱歉应用程序意外停止”。我不知道为什么。有人可以帮帮我????

4 个答案:

答案 0 :(得分:0)

您无法在与UI线程不同的线程内为yuor ImageView设置资源。 你可以使用runOnUiThread。它需要一个可运行的参数,并将其发布在UI线程队列中。在那里,UI thead接受并更新您的ImageView。总而言之,你的可运行将成为:

private class WelcomeScreen implements Runnable {

@Override
public void run() {


        try {
            for (int i = 0; i < imgID.length; i++)
            {
                final int resuorceId = imgID[i];
                runOnUiThread(new Runnable() {

                      @Override
                      public void run() {
                         imgView.setImageResource(resuorceId);
                      }
                });

                sleep(500);
            }

        } catch (InterruptedException e) {

        }finally {
            Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
            startActivity(intent);
            finish();
        }


}

答案 1 :(得分:0)

您无法从Thread访问您的视图。 你需要把你的代码imgView.setImageResource(imgID [i]);在runOnUiThread中

使用像:

runOnUiThread(new Runnable(){

        @Override
        public void run() {
            // TODO Auto-generated method stub
            imgView.setImageResource(imgID[i]);
        }
    });

由于

答案 2 :(得分:0)

您无法在非UI线程中更改UI中的内容,因此请将此代码替换为:

imgView.setImageResource(imgID[i]);

为:

runOnUiThread(new Runnable() {
   @Override
   public void run() {
        imgView.setImageResource(imgID[i]);
   }
});

答案 3 :(得分:0)

//try code this way...
public class SplashScreen extends Activity {

private Intent launchIntent;
private Thread splashThread;                    //used for perform splash screen operation

private int splashTime = 10000, sleepTime = 50; //used for threading operation
private boolean active = true;                  //used for touch event

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashscreen);                                  //Set splashscreen.xml here

    try {           
            splashThread = new Thread() {                                   // Creating Thread for splash the screen

            @Override
            public void run() {                                             // run method implemented to perform threading operation
                try {
                    int waitTime = 0;                                       //counter for threading
                    do {
                        sleep(sleepTime);                                   //delay for specific time
                        if (active)
                            waitTime += 100;


                    //write your image code here that display your no. of images



                    } while (active && (waitTime < splashTime));            //Check touch condition and counter

                } catch (Exception e) {
                    // to handle runtime error of run method                        
                    Validation.displayToastMessage(SplashScreen.this, e.toString());    //Call static method of class ToastMessage
                }
                finish();                                                               //finish current activity
                startJustCoupleActivityScreen();                                        //Call below defined function
            }
        };
        splashThread.start();                                                           //start thread here
    } catch (Exception e) {
        message("SplashScreen : "+ e.toString());               //Call static method of class ToastMessage
    }

}

public void startJustCoupleActivityScreen() {
    launchIntent=new Intent(SplashScreen.this,JustCoupleActivity.class);    //call Next Screen
    startActivity(launchIntent);                                            //start new activity
}

@Override
public boolean onTouchEvent(MotionEvent event) {                            //onTouch Event
    //on touch it immediate skip splash screen 
    if(event.getAction()==MotionEvent.ACTION_DOWN) active=false;            //Check Touch happened or not
    return true;
}

public void message(String msg)
{
    Validation.displayToastMessage(SplashScreen.this, msg);     //display Error Message
}

}