关于线程!!
关于线程方面,我的代码如下,找不出什么不妥的地方,但虚拟器报错:”unfortrunly,XXX中止运行。”
求解!谢谢!
public class Start extends Activity implements Runnable{
private Thread thread; //声明线程对象
private int imgNum=1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//全屏模式,屏蔽一切标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.start);
thread=new Thread(Start.this); //创建一个线程
thread.start(); //开启线程
}
public void run() {
//获取动画资源
Animation anim_start=AnimationUtils.loadAnimation(this, R.anim.a_startalpha);
while(!Thread.currentThread().isInterrupted()&& imgNum<=3){
switch(imgNum){
case 1:
ImageView iv1=(ImageView)findViewById(R.id.start1);//获取要应用动画效果的ImageView
iv1.setBackgroundResource(R.drawable.start1);//取得第一幅背景图
iv1.startAnimation(anim_start); //开始播放第一幅
try {
thread.sleep(2000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
break;
case 2:
ImageView iv2=(ImageView)findViewById(R.id.start2);//获取要应用动画效果的ImageView
iv2.setBackgroundResource(R.drawable.start2);//取得第二幅背景图
iv2.startAnimation(anim_start); //开始播放第二幅
try {
thread.sleep(2000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
break;
case 3:
ImageView iv3=(ImageView)findViewById(R.id.start3);//获取要应用动画效果的ImageView
iv3.setBackgroundResource(R.drawable.start3);//取得第三幅背景图
iv3.startAnimation(anim_start); //开始播放第三幅
try {
thread.sleep(2000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
break;
default:
}
imgNum++;
}
}
protected void onDestroy() {
if(thread!=null){
thread.interrupt(); //中断线程
thread=null;
}
super.onDestroy();
}
}