I know this question has been asked a million times because I have done some research and have found many threads on this. I have tried to use the answers in those threads but I am having a bit of trouble.
I am looking to set a few variables that I can use across all of my activities.
I created a GlobalVariables.java class which looks like the following (the value in there is just for testing purposes as of now):
import android.app.Application;
public class GlobalVariables extends Application {
int holeAmount;
public int getHoles(){
return holeAmount;
}
public void setHoles(String s){
holeAmount = 30;
}
}
in my main activity where everything is happening I have the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GlobalVariables global = ((GlobalVariables)getApplicationContext());
int totalHoles = global.getHoles();
and on the "GlobalVariables global = ..." line I am getting the following error:
Multiple markers at this line
- GlobalVariables cannot be resolved
to a type
- GlobalVariables cannot be resolved
to a type
I tried to follow the instructions here but clearly I am doing something incorrectly. > How to declare global variables in Android?
Any help would be greatly appreciated!
Thanks!
SECOND ATTEMPT:
EasyPar.java (Errors @ EasyParHelperActivity)
package com.movi.easypar;
import java.text.DateFormat;
import java.util.Date;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
public class EasyPar extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
(initialize all my buttons and textviews)
}
public void someMethod() {
EasyParHelperActivity.helper.setHoles(30);
int holes = EasyParHelperActivity.helper.getHoles();
}
(the rest of my button functions, etc.)
EasyParHelperActivity (No Errors)
package com.movi.easypar;
import android.app.Application;
public class EasyParHelperActivity extends Application {
public static EasyParHelper helper = new EasyParHelper();
}
EasyParHelper.java (No Errors)
package com.movi.easypar;
public class EasyParHelper {
private int holeAmount = 0;
public int getHoles() {
return holeAmount;
}
public void setHoles(int holes) {
holeAmount = holes;
}
}
All i want is for the user to be able to click a button "18" or "9" on the first screen, and for the application to be able to use that value other times throughout whatever it does. So I need to set it in screen 1, and in screen 2 i need to retrieve that value.