简体   繁体   中英

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;
}


}

use

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

instead of

display.setText(counter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM