【问题标题】:Simple Android Timer Game简单的安卓计时器游戏
【发布时间】:2014-07-22 14:27:39
【问题描述】:

我知道有一些 Android Timer 帖子,但我真的看不出我在其中缺少什么。

用户按下一个按钮开始游戏,文本会随着经过的秒数更新,如果在八秒后按下该按钮,则会显示一条祝贺消息。

public static class PlaceholderFragment extends Fragment {

    public static long startTime;
    public static int time;
    public static boolean buttonClicked1;
    public static boolean buttonClicked2;
    public static boolean win;
    public static TextView todd;
    public static TextView tim;
    public static Button bob;

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        win = false;
        bob = (Button)rootView.findViewById(R.id.button);
        tim = (TextView)rootView.findViewById(R.id.message);

        todd = (TextView)rootView.findViewById(R.id.timer);
        startTime = System.currentTimeMillis();
        time = 0;
        buttonClicked1 = false;
        buttonClicked2 = false;

        bob.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                if(!buttonClicked1){
                    buttonClicked1 = true;
                    bob.setText("Push Here");
                    play();
                }
                else if(buttonClicked1) //Has game started
                {
                    if(time>8)
                        buttonClicked2 = true;

                }                   
            }
        });

        return rootView;
    }

    public void play(){
        playThread paul = new playThread();
        paul.run();
    }

    public void winner(){
         tim.setText("Congratulations");
    }

    public boolean checkWin(){
        return buttonClicked2 ? true : false;
    }

    public void updateTime(){
        if((System.currentTimeMillis() - startTime) % 1000 > 0){
            startTime = System.currentTimeMillis();
            time++;
        }
        todd.setText(""+time);
    }

    private class playThread implements Runnable{
        public void run(){
            System.out.println("Thread running");
            while(!win){
                updateTime();
                win = checkWin();
                winner();
            }
        }
    }
}

}

在阅读this 之后,我决定实现 Runnable 而不是为 playThread 私有类扩展 Thread。

点击开始按钮后游戏变得无响应,其文本不会变为“Push Here”。

当然,我是一个完整的 Android 初学者,我怀疑问题出在我的线程实现上,但问题可能出在其他地方,因为我不太熟悉编程风格或约定。

感谢您查看此内容!

【问题讨论】:

    标签: java android multithreading timer


    【解决方案1】:

    您的playThread 类,尽管它的名字,不会创建一个新线程。它在主 GUI 线程中运行,并包含一个挂起您的应用程序的循环。创建此类的实例并调用其 run 方法只是在主线程中运行该代码。您应该创建一个线程并在该单独线程中运行该实例。

    【讨论】:

    • 如果他在一个实际的单独线程上启动线程,他将在winner() 中得到一个CalledFromWrongThreadException
    • 我以为 Runnable 类就像线程一样,单独运行,不会导致应用挂起。如何在不循环的情况下重复更新时间?
    • Thread thread = new Thread(new playThread()); thread.start(); 在单独的线程上启动 Runnable
    • @Nick Runnable 它是一个可以传递给 Thread 实例的接口,当该线程启动时,它将在新 Thread 中运行其 run 方法。
    • 我会提供更多帮助,但我遇到了这种方法的问题,并直接将计时器绘制到 SurfaceView 的画布上,而不是乱用 TextView。正如我所说,即使您启动线程,winner() 也会从另一个线程访问 TextView,而不是 UI 线程(这会导致 CalledFromWrongThreadException )。您当然可以致电runOnUiThread(new Runnable() { winner(); });,但肯定有比这更好的方法,因此我放弃了在这样的用例中显示文本视图。 (我在写游戏)
    猜你喜欢
    • 1970-01-01
    • 2015-01-01
    • 2014-03-20
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    相关资源
    最近更新 更多