0

我写了一个启动画面在应用程序启动时运行

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

4 回答 4

0

您不能在ImageView与 UI 线程不同的线程内为 yuor 设置资源。你可以使用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();
        }


}
于 2013-04-26T10:17:36.803 回答
0

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

像这样使用:

runOnUiThread(new Runnable() {

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

谢谢

于 2013-04-26T10:18:27.340 回答
0

您不能从非 UI 线程更改 UI 中的某些内容,因此请替换您的代码:

imgView.setImageResource(imgID[i]);

至:

runOnUiThread(new Runnable() {
   @Override
   public void run() {
        imgView.setImageResource(imgID[i]);
   }
});
于 2013-04-26T10:18:54.983 回答
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
}

}
于 2013-04-26T10:22:01.587 回答