0
package your.splash.namespace;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;

public class SplashScreenActivity extends Activity{

protected boolean _active = true;
protected int _splashTime=1500;  //The timeout 
TextView tv;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv=(TextView) findViewById(R.id.stext);

   Thread splashThread = new Thread()
    {
        @Override
        public void run()
        {
            try
            {
                int waited=0;

                      //the thread will stop if _active==false and bigger than _splashTime
                while(_active && (waited < _splashTime))
                {
                    sleep(100);

                    if(_active)
                    {
                        waited +=100;
                    }
                }
            }catch(InterruptedException e)
            {

            }
            finally
            {
                tv.setText("how"); //when it is timeout, the text will change to this one
            }
        }


    };

    splashThread.start();
}

//When user press screen, it set _active to false to stop the thread
public boolean onTouchEvent(MotionEvent event)
{
   if(event.getAction()==MotionEvent.ACTION_DOWN)
   {
       _active = false;
   }

   return true;
}
}

スレッドがタイムアウトしたときに、Textの「方法」をTextViewに設定したいだけです。このプログラムはEclipseで実行できます。ただし、スレッドがタイムアウトすると、「残念ながら、SplashScreenが停止しました」というポップアップエラーメッセージが表示され、[OK]ボタンを押した後にプログラムが終了(停止)します。なにが問題ですか?直し方?

4

1 に答える 1

1
 tv.setText("how"); is in onon UI thread...

UIスレッド以外でUI関連の作業を行うことはできません...

finally
            {


runOnUiThread(new Runnable() {
    public void run() {
       tv.setText("how");
    }
});

}
于 2012-06-26T19:36:51.777 に答える