Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6870539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:41:45+00:00 2026-05-27T03:41:45+00:00

When I try to use the onClickListener method for a button, variable outside of

  • 0

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");

  }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:41:46+00:00Added an answer on May 27, 2026 at 3:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to work with DirectoryInfo, FileInfo with very long path. I try use
as soon as I use method Game(), my app crashes, why? Also I'd like
I try to use this code for toggling text on the link: var showText=<span>Open</span>
I try to use the domainpeople.com API and to do I need to use
I try to use doctest from example from http://docs.python.org/library/doctest.html But when I run python
I try to use the winDBG to debug a dump file. When I run
I try to use the following code: ArrayList<String> Map<String, String> Eclipse complains about both
I try to use PHPMailer to send registration, activation. etc mail to users: require(class.phpmailer.php);
I try to use single quotes as much as possible and I've noticed that
I try to use amazon API using PHP. If I use print_r($parsed_xml->Items->Item->ItemAttributes) it show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.