1

I have a very simple application with two buttons and a text. I press one and the number on the text goes up. The other makes the number decrease. Here is my code.

package com.bryantpc.itemcounter;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.bryantpc.itemcounter.R;

public class IC_MAIN extends ActionBarActivity {


 private int Amount1 = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   final Button Button1 = (Button)findViewById(R.id.button1);       
   final Button Button2 = (Button)findViewById(R.id.button2);       

   final TextView TextView1 = (TextView)findViewById(R.id.textView1);      

   Button1.setOnClickListener(new OnClickListener() {
       public void onClick(View v) {      
           Amount1++;
           TextView1.setText(Amount1);
       }
   });

   Button2.setOnClickListener(new OnClickListener() {
        public void onClick (View v) {
            Amount1--;
            TextView1.setText(Amount1);
        }
    });

Every time when I press one of the buttons, the app stops working. Anyone knows what's going on? P.S I have no LogCat because my emulator is not working I am running the APK on my phone.

Vincent Thacker
  • 393
  • 1
  • 13

1 Answers1

2

You can't do TextView1.setText(Amount1) unless that is a resource ID, which in this case it isn't. Use TextView1.setText(String.valueOf(Amount1)) when setting the text.

As an aside, you should name your classes and variables according to something like https://source.android.com/source/code-style.html

Dororo
  • 3,420
  • 2
  • 30
  • 46