0

I am implementing a click counter variable to keep track of how many times a button is clicked and to do something each time it is clicked. I am implementing it as the following code shows.

    package com.mycompany.myapp3;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import static java.lang.System.out;
import java.util.Random;
import android.view.View.*;
import android.widget.*;

public class MainActivity extends Activity{
public boolean onClick;
public int count=0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final TextView textView1=(TextView)findViewById(R.id.textView1);
    final Button myButton=(Button)findViewById(R.id.btn);
    myButton.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            count++;

            if(onClick=true && count==1){
                textView1.setText("Hello");
                //Activity performed onClick
                }
            }
        });
    }
} 

Whenever I try to remove the final button and replace it with a button my app crashes. How do I implement the variable into more than one button(eg not having a final button, only a button)?

(Forgive me if you are seeing this before I add the code. For some reason the toolbar does not show up until I ask the question and edit it.)

Jax
  • 402
  • 7
  • 24
  • why do you want "remove and replace button" ? – klimat Dec 22 '14 at 16:40
  • I might not be explaining right. I want multiple buttons, but as it stands with a final button, when I make multiple buttons error messages pop up and my app crashes. – Jax Dec 22 '14 at 16:42
  • It says the value of my other final buttons is null. If I delete the "final" then my code has innumerable errors and my entire main activity is messed up. This in turn effects my other activities as well as my other class files that rely on my main activity. – Jax Dec 22 '14 at 16:44
  • Explain me that from user point of view. What user see in window? What user will see when button will be pressed? – klimat Dec 22 '14 at 16:48
  • you can refer to http://stackoverflow.com/questions/11424753/why-do-variables-passed-to-runnable-need-to-be-final – jzyamateur Dec 22 '14 at 17:22
  • When the button is pressed, The app freezes and crashes. I have searched the API and tested on my 4.0 phone and 4.4 tablet so it can't be API (I initially encountered the problem on my phone). Eclipse repeatedly says errors are there. – Jax Dec 22 '14 at 17:34
  • @jzyamateur Thanks for the help, that cleared some of my confusion up ;) – Jax Dec 22 '14 at 17:38

1 Answers1

0

Declare the button in the class scope instead of the onCreate

int count=0;
Button myButton;

Then initialize it and use it in your onCreate

myButton = findViewById(..);

This will make it available everywhere in the class

Shooky
  • 1,269
  • 8
  • 16
  • For some reason this method worked fine on the emulator but when I finally got around to installing it on my phone it crashed when I opened the app. – Jax Jan 05 '15 at 18:09
  • can you post the stacktrace? there is no reason this should cause a crash on device but not on emulator – Shooky Jan 05 '15 at 21:22