3

When I try to use the onClickListener method for a button, variable outside of any onCreate or onPause or onAnything method, it does not work. I also cannot even set the value of a button variable outside of an "onAnything" method. Help would be great.

Thanks!

public class StartingPoint extends Activity {
/** Called when the activity is first created. */

int counter;
Button add= (Button) findViewById(R.id.bAdd);
Button sub= (Button) findViewById(R.id.bSub);
TextView display= (TextView) findViewById(R.id.tvDisplay);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    Log.i("phase", "on create");
    counter=0;       

    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter++;
            display.setText(""+counter);
            display.setTextSize(counter);
            Log.i("phase", "add");
        }
    });
    sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter--;
            display.setText(""+counter);
            display.setTextSize(counter);
            display.setTextColor(Color.GREEN);
            Log.i("phase", "sub");
        }
    });

}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    Log.i("phase", "on start");
    SharedPreferences prefs = getPreferences(0); 
    int getfromfile = prefs.getInt("counter_store", 1);
    counter=getfromfile;
    display.setText(""+getfromfile);
    display.setTextSize(getfromfile);
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    Log.i("phase", "on stop");
     SharedPreferences.Editor editor = getPreferences(0).edit();
     editor.putInt("counter_store", counter);
     editor.commit();
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    counter=0;
    Log.i("phase", "on destroy");

  }

}
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
user947659
  • 2,485
  • 4
  • 21
  • 24

2 Answers2

4

One thing I notice about your code is that you are initializing your views when you declare them, which is before you set the content view. Find view by id will not work until after you do so. Change it so you declare them like

Button add;
Button sub;

...
    setContentView(R.layout.main);
    add = (Button) findViewById(R.id.bAdd);
    sub = (Button) findViewById(R.id.bSub);

Also, the error message you are seeing is because you cannot execute that statement outside of a method. You should be doing it inside onCreate anyway.

skynet
  • 9,898
  • 5
  • 43
  • 52
  • 2
    Why does it not work outside of a method? Sorry, I want to learn about it instead of taking it at face value. Thanks! – user947659 Nov 30 '11 at 04:51
  • For the same reason that you cannot call any method outside of a method in Java (apart from initializing variables). Besides, you want to set the `onClickListener` in `onCreate`, after you call `setContentView` which allows you to use `findViewById` to get the buttons. – skynet Nov 30 '11 at 04:56
3

You should go thorugh this blog - Different Ways To Handle Clicks

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242