I am new to android development and have been doing some practices. I did some research and debugging and believe i found the source of the problem. Which i believe is this portion here. My aim is to create two buttons that add 1 and deduct 1 upon click and displaying the sum.
public class MainActivity extends ActionBarActivity {
int counter;
Button add,sub;
TextView Display;
@Override
protected 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.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
Display.setText("Your total is "+ counter);
}
});//Take it as one liner
sub.setOnClickListener(new View.OnClickListener() {
//using the id of the buttons
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
Display.setText("Your total is " + counter);
}
});//Take it as one liner
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
But i cannot seem to figure out what's wrong. I am very sure that the problem lies with the onClickListener as it only crashes when i implement the code and i am very sure i used the correct id.
i put my button id under strings. Am i wrong to do that?