刚刚开始学习android程序的开发,参照教材自行写了一个“幸运8”的游戏,可一运行手机就会弹出“幸运8已停止运行”的提示,请各位大神教教小弟代码哪里出错了···感激万分package com.luck8;
import com.luck8.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Luck8Activity extends Activity {
/** Called when the activity is first created. */
Button button1=(Button)findViewById(R.id.button2);
Button button2=(Button)findViewById(R.id.button1);
TextView text1=(TextView)findViewById(R.id.textView1);
TextView text2=(TextView)findViewById(R.id.textView2);
TextView text3=(TextView)findViewById(R.id.textView3);
Thread t;
int x,y,z;
boolean RUN =false;
//----------------以上是获取各个按钮和textView的实例
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
start(); //开始按钮监听事件
}
});
button2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
stop(); //停止按钮监听事件
}
});
}
protected void stop() {
// TODO Auto-generated method stub
RUN = false;
t = null; //停止方法
}
protected void start() {
// TODO Auto-generated method stub
RUN = true;
t = new Thread();
t.start(); //开始方法
}
@SuppressWarnings("static-access")
public void run(){ //线程的run方法
while(RUN){
x = (int)(Math.random()*10);
y = (int)(Math.random()*10);
z = (int)(Math.random()*10);
text1.setText(String.valueOf(x));
text2.setText(String.valueOf(x));
text3.setText(String.valueOf(x));
try{
t.sleep(1000); //线程等待
}catch(InterruptedException e){
e.printStackTrace();
}
}
}}
添加回答
举报
0/150
提交
取消