Question

I have been coding a timer on my app so that when you hit a button the text displays a time which counts up in 0.1 second increments.

I though i coded it right but it turns out the app crashes whenever i hit the ready button on my phone... Could someone give me a hint as to what is going wrong. I think it could be something with starting the timer?

My 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){


                    }

                }
            });


        }
    });


}
Était-ce utile?

La solution

I can see a problem in your line:

text.setText(0);

The declaration of function is setText(int resid) so it tries to search for a string resource with id 0 and no will be found, stack trace error:

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

So replace your line with:

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

Second thing, you have declared your number variable as int:

int number = 0;

But, you're adding a float number to it:

number+=0.1;

So declare it float number = 0;.

To round up your number to one decimal place use this trick with mutliplying the number first, converting it to int to strip away decimal places and converting it back to float:

float displaynumber = (float)((int)(number * 10.0))/10.0f;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top