1

我一直在我的应用程序上编写一个计时器,这样当你点击一个按钮时,文本会显示一个以 0.1 秒为增量计数的时间。

我虽然编码正确,但事实证明,每当我按下手机上的就绪按钮时,应用程序就会崩溃......有人可以给我一个提示,说明出了什么问题。我认为这可能与启动计时器有关?

我的 OnCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {



    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_double_player);

    Button readyBtn =  (Button) findViewById(R.id.readyBtn);



    readyBtn.setOnClickListener(new View.OnClickListener(){
        public void onClick(View arg0) {



            Button readyBtn =  (Button) findViewById(R.id.readyBtn);
            Spinner selectorLeft = (Spinner) findViewById(R.id.selectorLeft);
            Spinner selectorRight = (Spinner) findViewById(R.id.selectorRight);
            ImageView narLeft = (ImageView) findViewById(R.id.narwhalleft);
            ImageView narRight = (ImageView) findViewById(R.id.narwhalRight);


            selectorLeft.setVisibility(RelativeLayout.GONE);
            selectorRight.setVisibility(RelativeLayout.GONE);
            readyBtn.setVisibility(RelativeLayout.GONE);

            final TextView text = (TextView) findViewById(R.id.timerText);
            final Handler handler;
            text.setText(0);


            handler = new Handler();

            Runnable runnable = new Runnable(){
                boolean ready = true;
                int number = 0;

                @Override
                public void run(){
                    while(ready){
                        try{
                            Thread.sleep(100);
                            }
                        catch(InterruptedException e){
                            e.printStackTrace();
                            }
                        handler.post(new Runnable(){
                            @Override
                            public void run(){
                                number+=0.1;
                                text.setText(String.valueOf(number));
                                }
                            });
                    }
                }
            };
            new Thread(runnable).start();

            narRight.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View arg0) {
                    int clicksNeeded = 100;
                    boolean rightWon = false;
                    int clicksClickedRight = 0;

                    clicksClickedRight = clicksClickedRight + 1;

                    if(clicksClickedRight == clicksNeeded){

                        rightWon = true;
                        }
                    if(rightWon == true){


                    }

                }


            });

            narLeft.setOnClickListener(new View.OnClickListener(){
                public void onClick(View arg0) {
                    int clicksNeeded = 100;
                    boolean leftWon = false;
                    int clicksClickedLeft = 0;

                    clicksClickedLeft = clicksClickedLeft + 1;

                    if(clicksClickedLeft == clicksNeeded){

                        leftWon = true;
                        }
                    if(leftWon == true){


                    }

                }
            });


        }
    });


}
4

1 回答 1

0

我可以在您的行中看到一个问题:

text.setText(0);

函数的声明是setText(int resid)这样它尝试搜索具有 id 的字符串资源0并且不会找到,堆栈跟踪错误:

android.content.res.Resources$NotFoundException: String resource ID #0x0

因此,将您的行替换为:

text.setText(String.valueOf(0));

第二件事,您已将 number 变量声明为 int:

int number = 0;

但是,您正在向其中添加一个浮点数:

number+=0.1;

所以声明它float number = 0;

要将您的数字四舍五入到小数点后一位,请使用此技巧,首先将数字相乘,将其转换为 int 以去除小数位并将其转换回浮点数:

float displaynumber = (float)((int)(number * 10.0))/10.0f;
于 2013-11-03T15:24:44.683 回答