问题描述
我知道这个问题已经被问了一百万次,因为我已经做了一些调查,发现许多线程这一点。我曾尝试在这些线程使用的答案,但我有一个有点麻烦。
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.
我创建了一个GlobalVariables.java类看起来像下面(的价值存在只是为了测试目的截至目前):
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();
和对GlobalVariables全球= ...行,我收到以下错误:
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
我想在这里按照说明进行操作,但显然,我错误地做一些事情。 >
任何帮助将大大AP preciated!
Any help would be greatly appreciated!
谢谢!
第二次尝试:
EasyPar.java (错误@ EasyParHelperActivity)
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 (无错误)
package com.movi.easypar;
import android.app.Application;
public class EasyParHelperActivity extends Application {
public static EasyParHelper helper = new EasyParHelper();
}
EasyParHelper.java (无错误)
package com.movi.easypar;
public class EasyParHelper {
private int holeAmount = 0;
public int getHoles() {
return holeAmount;
}
public void setHoles(int holes) {
holeAmount = holes;
}
}
我要的是用户能够点击的第一个屏幕上的按钮,18或9为,并为应用程序能够使用该值其他时间全不管它。所以,我需要将其设置为1的屏幕,并在屏幕2我需要检索该值。
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.
推荐答案
创建一个助手类,如下所示...
Create a 'helper' class as follows...
package com.my.application
public class MyAppHelper {
private int holeAmount = 0;
public int getHoles() {
return holeAmount;
}
public void setHoles(int holes) {
holeAmount = holes;
}
}
然后为你的应用程序类执行下列操作...
Then for your Application class do the following...
package com.my.application
public class MyApplication extends Application {
public static MyAppHelper helper = new MyAppHelper();
}
要访问你可以简单地调用助手的get / set方法...
To get access to the get/set methods in the helper you can simply call...
package com.my.application
public class MyActivity extends Activity {
// Normal onCreate(...) etc here
public void someMethod() {
MyApplication.helper.setHoles(30);
int holes = MyApplication.helper.getHoles();
}
}
这篇关于使用全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!