bot攻略 2014-09-12 04:37 采纳率: 100%
浏览 1933
已采纳

初学android写的一个通过线程实现“幸运8”的游戏遇到个问题

刚刚开始学习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();
        }
    }
}

}

  • 写回答

1条回答

  • 大虫子猪NO1 2014-09-12 10:04
    关注

    控件的初始化要放在onCreate()中
    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);
    改为
    Button button1;
    Button button2;
    TextView text1;
    TextView text2;
    TextView text3;
    然后在setContentView(R.layout.main);下面写:
    button1=(Button)findViewById(R.id.button2);
    button2=(Button)findViewById(R.id.button1);
    text1=(TextView)findViewById(R.id.textView1);
    text2=(TextView)findViewById(R.id.textView2);
    text3=(TextView)findViewById(R.id.textView3);
    最后,别忘了在Manifest中注册你的Luck8Activity

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记