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 6771749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:28:08+00:00 2026-05-26T15:28:08+00:00

How do or where do I define variables that can be used throughout an

  • 0

How do or where do I define variables that can be used throughout an activity? I have three onClick Listeneners that contain a bunch of if else statements within each. One onClick causes a bunch a calaculations to occur, another onClick saves results of the calculations using out.write instructions. I am having a problem getting double variables from within if else calcuations of an onClick routine to be known to the save onClick portion of my code. I have been reading books and looking for answers but nothing seems to work. I am using a breakpoint to stop before an out.write that contains a variable from my execution onClick routine but they are unknown values. I first thought the variables need to be global but I just need them to be available throughtout the activity.

I have read plenty of books and tried to comprehend but I jumped into java and wrote 3000+ lines. Everything works as far as getting input, deciding what the inputs were, and displaying the results. I even create a nice .xml file on the sdcard showing all the data results. The problem is when I try and put a variable in the out.write code the variable m1_sqs1_eng is unknown in that onCreate routine. I had my variables defined like your example but it did not work. Some code example of my problem:

public class MyActivity extends Activity {
    private EditText m1_sqs1;
    private Spinner m1_sqs1_spinner;
    private double m1_sqs1_eng;
    private double m1_sqs1_value;

    protected onCreate( Bundle savedBundle ) {
       m1_sqs1_spinner = (Spinner) findViewById(R.id.m1_sqs1_spinner);
       m1_sqs1 = (EditText) findViewById(R.id.m1_sqs1);

        btnExecute.setOnClickListener(new View.OnClickListener() {
      if (((m1_sqs1_spinner.getSelectedItem().toString().equals("in")))) {
       double m1_sqs1_eng = new Double(m1_sqs1.getText().toString());
       });

    btnSave.setOnClickListener(new View.OnClickListener() {
      public void onClick(View arg0) {
      m1_sqs1_value = m1_sqs1_eng;
      out.write ("    <Cell ss:StyleID=\"s44\"><Data ss:Type=\"Number\">" + m1_sqs1_value + "</Data><NamedCell ss:Name=\"Print_Area\"/></Cell>\r\n");
       });
    }
}
  • 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-26T15:28:08+00:00Added an answer on May 26, 2026 at 3:28 pm

    For example:

    public class MyActivity extends Activity {
        private EditText streetField;
        private EditText nameField;
    
        protected onCreate( Bundle savedBundle ) {
           streetField = findViewById( R.id.streetField );
           nameField = findViewVById( R.id.nameField );
    
           someButton.setOnClickListener( new View.OnClickListener() {
               public void onClick( View view ) {
                   Person person = new Person();
                   person.setName( nameField.getText().toString() );
                   person.setStreet( streetField.getText().toString() );
               }
           });
        }
    }
    

    Here streetField and nameField are instance variables for the MyActivity class. They can be used by in any instance method, inner class, or anonymous inner classes defined in instance methods. You probably want to pick up a introductory Java book because most of this is discussed in those books.

    http://java.sun.com/docs/books/effective/

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

Sidebar

Related Questions

Is there any way to define a variable that can be used in multiple
Can you define a macro that accesses a normal variable, but in a read-only
I have a variable that im define with another variable and some text. $title
I always used a normal PHP file and just defined the variables in that
I would like to have a variable (or #define ) in C++ source that
My database tables 1 row looks like that I'm storing all variables that used
In C, you can define structures to hold an assortment of variables; typedef struct
I have a number of non-page content items that are used as callouts on
I want to define a class method that has access to a local variable.
Python, C++, Scheme, and others all let you define functions that take a variable

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.