What am I missing in the following buttons code?

Posted by Ayush Goyal on Stack Overflow See other posts from Stack Overflow or by Ayush Goyal
Published on 2012-06-20T03:02:11Z Indexed on 2012/06/20 3:16 UTC
Read the original article Hit count: 324

Filed under:
|

I am trying to increment and decrement the middle textview via buttons on the sides. The application starts up finely but by the time I click on any of the buttons it gets closed with following error.
Error: process <package> has stopped unexpectedly.

My main.xml:

<?xml version="1.0" encoding="utf-8"?>

<Button
    android:id="@+id/button1"
    android:layout_width="50dp"
    android:layout_height="250dp"
    android:text="+"
    android:textSize="40dp" />

<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    android:textSize="80dp"
    android:layout_toRightOf="@+id/button1"
    android:layout_marginTop="75dp"
    android:layout_marginLeft="80dp"
     />


<Button
    android:id="@+id/button2"
    android:layout_width="50dp"
    android:layout_height="250dp"
    android:layout_alignParentRight="true"
    android:text="-"
    android:textSize="40dp" />

My java file:

public class IncrementDecrementActivity extends Activity {

int counter;
Button add, sub;
TextView tv;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    add = (Button) findViewById(R.id.button1);
    sub = (Button) findViewById(R.id.button2);
    tv = (TextView) findViewById(R.id.tv1);

    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter++;
            tv.setText(counter);
        }
    });

    sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter--;
            tv.setText(counter);
        }
    });

}

}

© Stack Overflow or respective owner

Related posts about android

Related posts about button