0

my application force closes but compiler doesn't show any errors at all i have provided the code

 package my.android;

package com.google.android.gms.auth.sample.helloauth;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;

public class HelloActivity extends Activity {
    /** Called when the activity is first created. */
    int time;
    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        time = 0;
        Timer t = new Timer();

        t.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        tv = (TextView) findViewById(R.id.tvDisplay);

                        tv.setText(time);
                        time += 1;
                    }
                });
            }
        }, 0, 10000);
    }
}
Carlos Robles
  • 10,828
  • 3
  • 41
  • 60

2 Answers2

0

SetText method asks for a String parameter. Try this

tv.setText(String.valueOf(time));
Zohra Khan
  • 5,182
  • 4
  • 25
  • 34
0

move this line before creating the object of timer after setContentView(R.layout.main);

tv = (TextView) findViewById(R.id.tvDisplay);

and change

tv.setText(time); 

to

tv.setText(String.valueOf(time));

coz setText() always takes String as a parameter u r passing an int thats why facing force close

Kaushik
  • 6,150
  • 5
  • 39
  • 54