3

I was trying to make a simple app in which clicking tapb Button increments the variable value of notaps and the reset Button sets it to 0. When I click on tapb it increments the value & clicking reset resets it but when I again click tabp it increments from the previous value.

Eg :

init value of notaps = 0; 

I click tabp 3 times and notaps value = 3

I click reset and notaps value = 0

I click tabp 3 times and notaps value = 4

 Button tapb = (Button)findViewById(R.id.tapb); Button reset = (Button)findViewById(R.id.reset); tapb.setOnClickListener( new Button.OnClickListener(){ int notaps = 0; @Override public void onClick(View v) { TextView taps = (TextView)findViewById(R.id.taps); notaps++; taps.setText(String.valueOf(notaps)); } } ); reset.setOnClickListener( new Button.OnClickListener() { @Override public void onClick(View v) { TextView taps = (TextView) findViewById(R.id.taps); int notaps=0; taps.setText(String.valueOf(notaps)); } } ); 
2
  • 1
    declare notaps globally... Commented Aug 5, 2015 at 17:09
  • int noTaps=0 is bad. That variable disappears after that method executes. Commented Aug 5, 2015 at 17:11

2 Answers 2

5

First, you have 2 instances of int named notaps that have nothing to do with each other. Your reset button does not set the right notaps variable.

Here's a snippet that should work.

 private int notaps; Button tapb = (Button)findViewById(R.id.tapb); Button reset = (Button)findViewById(R.id.reset); TextView taps = (TextView)findViewById(R.id.taps); tapb.setOnClickListener( new Button.OnClickListener(){ @Override public void onClick(View v) { notaps++; taps.setText(String.valueOf(notaps)); } } ); reset.setOnClickListener( new Button.OnClickListener() { @Override public void onClick(View v) { notaps = 0; taps.setText(String.valueOf(notaps)); } } ); 
Sign up to request clarification or add additional context in comments.

1 Comment

I did a stupid mistake i declared int notaps in side onCreate
-1

Try declaring your notaps variable globally meaning where every you declare you buttons declare your notaps variable there, here is what I mean.

Button tapb = (Button)findViewById(R.id.tapb); Button reset = (Button)findViewById(R.id.reset); int notaps = 0; // declare variable globally tapb.setOnClickListener( new Button.OnClickListener(){ @Override public void onClick(View v) { TextView taps = (TextView)findViewById(R.id.taps); taps.setText(String.valueOf(++notaps)); } } ); reset.setOnClickListener( new Button.OnClickListener() { @Override public void onClick(View v) { TextView taps = (TextView) findViewById(R.id.taps); taps.setText(String.valueOf(notaps=0)); } } ); 

6 Comments

Don't declare it volatile. You're not going to access the variable from multiple threads, only the UI thread.
I'm not the best in android, but i thought that the listeners were callbacks from another thread. So therefore I declared the variable volatile, my bad if i was wrong.
The callbacks are called on the UI thread, by the buttons. Every UI interaction happens on the UI thread ("main" thread).
All I can say is this: keep up the good work and continue experimenting and learning! ;)
Yes absolutely, however ultimately it depends the actual machine, if the machine architecture is such that when a thread is created it is give it's own cache, then volatile saves the day because, what volatile does is it allows a shared copy of the variable you are accessing, but again some machine architectures don't have this problem volatile is there for the sake of portability.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.