簡體   English   中英

啟動畫面無法與線程一起使用

[英]Splash Screen not working with Thread

我寫了一個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();
            }


    }
}

}

出現錯誤“很抱歉,應用程序意外停止”。 我不知道為什么。 有人可以幫助我嗎?

您不能在與UI線程不同的線程內為您的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();
        }


}

您無法從Thread訪問您的視圖。 您將需要放置代碼imgView.setImageResource(imgID [i]);。 在runOnUiThread中

用途像:

runOnUiThread(new Runnable(){

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

謝謝

您無法通過非UI線程更改UI中的某些內容,因此請替換以下代碼:

imgView.setImageResource(imgID[i]);

至:

runOnUiThread(new Runnable() {
   @Override
   public void run() {
        imgView.setImageResource(imgID[i]);
   }
});
//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
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM