Android线程错误消息 打包您的.splash.namespace; 导入android.app.Activity; 导入android.os.Bundle; 导入android.view.MotionEvent; 导入android.widget.TextView; 公共类活动扩展了活动{ 受保护布尔值_active=true; 受保护的int _splashTime=1500;//超时 文本视图电视; 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); tv=(TextView)findViewById(R.id.stext); 线程飞溅线程=新线程() { @凌驾 公开募捐 { 尝试 { int=0; //如果_active==false且大于_splashTime,线程将停止 while(_active&&(waiting
我只想在线程超时时将文本“how”设置为TextView。这个程序可以在Eclipse中运行。但是,当线程超时时,弹出错误消息“不幸的是,SplashScreen已停止”,然后在我按下OK按钮后程序退出(停止)。怎么了?如何修复Android线程错误消息 打包您的.splash.namespace; 导入android.app.Activity; 导入android.os.Bundle; 导入android.view.MotionEvent; 导入android.widget.TextView; 公共类活动扩展了活动{ 受保护布尔值_active=true; 受保护的int _splashTime=1500;//超时 文本视图电视; 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); tv=(TextView)findViewById(R.id.stext); 线程飞溅线程=新线程() { @凌驾 公开募捐 { 尝试 { int=0; //如果_active==false且大于_splashTime,线程将停止 while(_active&&(waiting,android,multithreading,Android,Multithreading,我只想在线程超时时将文本“how”设置为TextView。这个程序可以在Eclipse中运行。但是,当线程超时时,弹出错误消息“不幸的是,SplashScreen已停止”,然后在我按下OK按钮后程序退出(停止)。怎么了?如何修复 package your.splash.namespace; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.
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;
}
}
除了UI线程,您不能在其他线程中执行与UI相关的工作
tv.setText("how"); is in onon UI thread...
finally
{
runOnUiThread(new Runnable() {
public void run() {
tv.setText("how");
}
});
}