Question

I've written this code for a simple Game. Basically, I want the player to press a button as many times as possible in 10 seconds. Everything works, and the code even gets Compiled, but when I start the App, it doesn't show me the score (the Text field is empty). What am I doing wrong?

public class ButtonActivity extends Activity implements OnClickListener {

private int score;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_button);
    new CountDownTimer(11000, 1000) {

         TextView TimerText = (TextView) findViewById(R.id.TimerText);

         public void onTick(long millisUntilFinished) {
             TimerText.setText("" + millisUntilFinished / 1000);
         }

         public void onFinish() {
            setContentView(R.layout.activity_score);
         }
    }.start();
}

private void screenUpdate() {
    TextView screenPoints = (TextView) findViewById(R.id.Scoringboard);
    screenPoints.setText(String.valueOf(score));
}

@Override
public void onClick(View view) {
    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(this);
    score++;
    screenUpdate();     
}

}
Was it helpful?

Solution

Try moving these two lines in the onCreate().

Button button =(Button) findViewById(R.id.button1);
button.setOnClickListener(this);

Basically onClick is called only if the click listener is attached...and you cannot attach it inside the onClick because it won't be called until is attached :). Moving the 2 lines inside the onCreate ensure the listener is attached.

OTHER TIPS

First you move this code on onCreate() method.

 Button button =(Button) findViewById(R.id.button1);
 button.setOnClickListener(this);
// try this way, hope this will help you...

**XML** code

**activity_button**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtTimer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginTop="10dp"/>

    <Button
        android:id="@+id/btnScore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Click For Score"/>

</LinearLayout>

**activity_score**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtScore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"/>

</LinearLayout>

**ACTIVITY** code

**ButtonActivity**
    // try this way, hope this will help you...

    **XML** code

    **activity_button**
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/txtTimer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_marginTop="10dp"/>

        <Button
            android:id="@+id/btnScore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Click For Score"/>

    </LinearLayout>

    **activity_score**
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/txtScore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textStyle="bold"/>

    </LinearLayout>

    **ACTIVITY** code

    **ButtonActivity**
    public class ButtonActivity extends Activity {

        private TextView txtScore ;
        private TextView txtTimer ;
        private Button btnScore;
        private int score;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_button);
            txtTimer = (TextView) findViewById(R.id.txtTimer);
            btnScore = (Button) findViewById(R.id.btnScore);
            new CountDownTimer(11000, 1000) {

                public void onTick(long millisUntilFinished) {
                    txtTimer.setText("Hurry!!!,You Have Only "+(millisUntilFinished / 1000)+" Second Left.");
                }

                public void onFinish() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            setContentView(R.layout.activity_score);
                            txtScore = (TextView) findViewById(R.id.txtScore);
                            txtScore.setText("Your Score Is >> "+score);
                        }
                    });
                }
            }.start();
            btnScore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    score++;
                }
            });
        }

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top