John
John

Reputation: 4028

Mistake in this small Java program

What is the mistake in this code? When I ran the program, it says "The application buttonProj (process com.example.buttonproj) has stopped unexpectedly. Please try again" I just tried to create an Add and Subtract button and user can click on it to increase or decrease number i.

  package com.example.buttonproj;

 import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

 import android.view.View;
 import android.widget.Button;
   import android.widget.TextView;


 public class MainActivity extends Activity {

int counter;
Button add, sub;
TextView display;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    counter = 0;

   add = (Button) findViewById(R.id.bAdd);
   sub = (Button) findViewById(R.id.bSub);
   display = (TextView) findViewById(R.id.textView1);

   add.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        counter +=1;
        display.setText(counter);
    }
});
   sub.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        counter--;
        display.setText(counter);


    }
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


}

Upvotes: 1

Views: 187

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

use

display.setText(""+counter);
 //OR
display.setText(String.valueOf(counter));

instead of

display.setText(counter);

Upvotes: 5

Related Questions