?
Solved

I can't get get a global variable to work in my Android App!

Posted on 2010-09-18
4
Medium Priority
?
1,152 Views
Last Modified: 2012-05-10
I can't get get a global variable to work in my Android App. I can get it to work within a method, but not outside of the method. I want to make my variables available to other methods. If I move my variables to just before the first method, the app stops working. After further tests, I have also found out that if I move all my variables to the beginning of the method, my app will stop as well. Finally after putting the variables to the original location, just before they are used, the app will run. I also noticed, however if I run debug when the app is working, that there are threads having difficulty being attached or something.

My questions are as follows:
How can I move my variables to be used globally and have the app run?
Why am I getting the thread error even when the app works, and how do I fix it?

please let me know if I need to provide more code! Debug error
package auto.speaker;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.ToggleButton;

public class Main extends Activity {
    /** Called when the activity is first created. */
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	// Variables
    	
    	// Code
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Create an instance of preferences to recall the data stored in it
        final SharedPreferences settings = getPreferences(0);
        // Create an instance of the toggle buttons and set them according to the values stored in preferences
        final ToggleButton toggleAS = (ToggleButton) findViewById(R.id.ToggleAutoSpeaker);
        toggleAS.setChecked(settings.getBoolean("AutoSpeakerEnabled", true));
        final ToggleButton toggleANP = (ToggleButton) findViewById(R.id.ToggleAutoNumPad);
        toggleANP.setChecked(settings.getBoolean("AutoNumPadEnabled", true));
        
        
        //Create an instance of the telephony manager
        //final TelephonyManager phoneState = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        //PhoneStateListener listener = new PhoneStateListener();
        //listener.onCallStateChanged(MODE_PRIVATE, TELEPHONY_SERVICE); //listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
        
        
        // Create an instance of the save button and listen to click events
        final Button saveButton = (Button) findViewById(R.id.saveButton);
        saveButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// Create an instance of editor to store the value of the toggle button
				SharedPreferences.Editor editor = settings.edit();
				editor.putBoolean("AutoSpeakerEnabled", toggleAS.isChecked());
				editor.putBoolean("AutoNumPadEnabled", toggleANP.isChecked());
				
				// Save the changes
				editor.commit();
				
				// Close the screen
				finish();
			}
		});
    }
    
    /*
    @Override
	protected void onPause()
	{
		super.onPause();
		
		stopListening();
	}
    
    @Override
	protected void onResume()
	{
		super.onResume();
		
		startSignalLevelListener();
	}*/
}

Open in new window

0
Comment
Question by:TheQuietShadow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Hegemon
ID: 33737481
There is no global variables in Java. If you want to share them among different classes or method, you will probably need them declared as class instance variables and in some cases static. You will need to modify the logic of your application accordingly.
0
 
LVL 6

Author Comment

by:TheQuietShadow
ID: 33737859
Okay... What about fields? isn't a field basically a global variable?
0
 
LVL 10

Accepted Solution

by:
Hegemon earned 1500 total points
ID: 33738120
Yes, a field or instance/member variable can be seen as a global variable for the class it is declared in. Whether it is visible/accessible for other classes depends on its visibility modifier: private, protected, public or no modifier.

Bear in mind that if it is NOT declared static, it is accessible only from the current INSTANCE of the class. Thus if your application creates more than one instance but you still need a sort of a global variable to be shared among them, you will probably need to declare it as

public static YourClassName yourVariable;

The above is a very coarse description, you will need to learn Java and OO fundamentals to use member variables properly.
0
 
LVL 6

Author Closing Comment

by:TheQuietShadow
ID: 33754976
Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 3 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question