0

基本上我有一个带有开始按钮的菜单类,它在线程中启动一个新的游戏活动。

在 Game 类中,我实例化了一个 Count 对象(它扩展了 CountDownTimer)。

启动游戏活动时,我收到 ANR 错误,我相信它是由计时器引起的,因为我已将其注释掉并且它工作得很好。坦率地说,我不知道如何解决这个问题;我尝试了不同的方法但失败了。 编辑后的代码*最后是 Logcat *

public class Menu extends Activity {

    Button start, highscores, exit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        addListenerOnButton();

    }

    private void addListenerOnButton() {

        start = (Button) findViewById(R.id.start);
        start.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {

                        startActivity(new Intent("net.fb.clauz.GAME"));

                };

        });

        highscores = (Button) findViewById(R.id.highscores);
        highscores.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {

            }
        });

        exit = (Button) findViewById(R.id.exit);
        exit.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {
                finish();
                System.exit(0);
            }
        });

    }
}



public class Game extends Activity {

    Button button;
    TextView score;
    public static TextView timeleft;
    int x = 0;
    public Counter count;
    public static boolean running=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        count = new Counter(30000, 1000);
        timeleft = (TextView) findViewById(R.id.timeleftTitle);

        gameThread = new Thread(new Runnable() {
            public void run() {
                while (running) {
                    addListenerOnButton();
                    count.start();
                }
            }
        });
        gameThread.start();
    }


    private void addListenerOnButton() {

        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {
                x++;
                score = (TextView) findViewById(R.id.score);
                score.setText(String.valueOf(x));

            }

        });

    }
}



public class Counter extends CountDownTimer{

    public Counter(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub
        Game.running = false; // stop the game

        //need to implement action to return to the buttons menu.
    }

    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub
        Game.timeleft.setText("Left: " + millisUntilFinished / 1000);
        System.out.println(millisUntilFinished / 1000);
    }

}

日志猫:

08-23 11:00:02.963: W/ActivityManager(885): Activity idle timeout for ActivityRecord{b4ca99d0 net.fb.clauz/.Game}

08-23 11:00:18.343: D/dalvikvm(14424): GC_CONCURRENT freed 3397K, 28% free 9419K/12935K, paused 4ms+4ms

08-23 11:00:21.383: I/InputDispatcher(885): Application is not responding: Window{b4cd2aa8 net.fb.clauz/net.fb.clauz.Game paused=false}.  5005.6ms since event, 5004.9ms since wait started

08-23 11:00:21.383: I/WindowManager(885): Input event dispatching timed out sending to net.fb.clauz/net.fb.clauz.Game

08-23 11:00:21.415: I/Process(885): Sending signal. PID: 14424 SIG: 3

08-23 11:00:21.415: I/dalvikvm(14424): threadid=3: reacting to signal 3

08-23 11:00:21.443: I/dalvikvm(14424): Wrote stack traces to '/data/anr/traces.txt'

08-23 11:00:21.443: I/Process(885): Sending signal. PID: 885 SIG: 3

08-23 11:00:21.443: I/dalvikvm(885): threadid=3: reacting to signal 3

08-23 11:00:21.593: I/dalvikvm(885): Wrote stack traces to '/data/anr/traces.txt'

08-23 11:00:21.593: I/Process(885): Sending signal. PID: 944 SIG: 3

08-23 11:00:21.593: I/dalvikvm(944): threadid=3: reacting to signal 3

08-23 11:00:21.634: I/dalvikvm(944): Wrote stack traces to '/data/anr/traces.txt'

08-23 11:00:21.634: I/Process(885): Sending signal. PID: 976 SIG: 3

08-23 11:00:21.634: I/dalvikvm(976): threadid=3: reacting to signal 3

08-23 11:00:21.673: I/dalvikvm(976): Wrote stack traces to '/data/anr/traces.txt'

08-23 11:00:22.463: D/dalvikvm(885): GC_CONCURRENT freed 472K, 14% free 11275K/13063K, paused 6ms+0ms

08-23 11:00:23.293: D/dalvikvm(885): GC_EXPLICIT freed 326K, 14% free 11328K/13063K, paused 6ms+9ms

08-23 11:00:23.963: E/ActivityManager(885): ANR in net.fb.clauz (net.fb.clauz/.Game)

08-23 11:00:23.963: E/ActivityManager(885): Reason: keyDispatchingTimedOut

08-23 11:00:23.963: E/ActivityManager(885): Load: 1.54 / 0.86 / 0.89

08-23 11:00:23.963: E/ActivityManager(885): CPU usage from 11149ms to 0ms ago:

08-23 11:00:23.963: E/ActivityManager(885):   98% 14424/net.fb.clauz: 96% user + 1.7% kernel / faults: 117 minor

08-23 11:00:23.963: E/ActivityManager(885):   0.6% 778/surfaceflinger: 0.6% user + 0% kernel

08-23 11:00:23.963: E/ActivityManager(885):   0.5% 885/system_server: 0.3% user + 0.1% kernel

08-23 11:00:23.963: E/ActivityManager(885):   0% 944/com.android.systemui: 0% user + 0% kernel / faults: 1 minor

08-23 11:00:23.963: E/ActivityManager(885): 99% TOTAL: 97% user + 1.9% kernel

08-23 11:00:23.963: E/ActivityManager(885): CPU usage from 1917ms to 2519ms later:

08-23 11:00:23.963: E/ActivityManager(885):   92% 14424/net.fb.clauz: 92% user + 0% kernel

08-23 11:00:23.963: E/ActivityManager(885):     46% 14424/net.fb.clauz: 46% user + 0% kernel

08-23 11:00:23.963: E/ActivityManager(885):     46% 14440/Thread-108: 46% user + 0% kernel

08-23 11:00:23.963: E/ActivityManager(885):   8.6% 885/system_server: 3.4% user + 5.1% 
kernel
08-23 11:00:23.963: E/ActivityManager(885):     6.8% 920/InputDispatcher: 3.4% user + 3.4% kernel

08-23 11:00:23.963: E/ActivityManager(885): 100% TOTAL: 95% user + 4.9% kernel

08-23 11:00:24.583: D/dalvikvm(885): GC_CONCURRENT freed 475K, 13% free 11423K/13063K, paused 6ms+15ms

08-23 11:00:38.693: W/ActivityManager(885):   Force finishing activity net.fb.clauz/.Game
4

1 回答 1

2

删除线程。

start.setOnClickListener(new OnClickListener() {

        public void onClick(View view) {


                    startActivity(new Intent("net.fb.clauz.GAME"));

        }
    });
于 2012-08-23T10:02:54.673 回答