Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从我的活动类访问变量_Android_Variables_Global Variables_Applicationcontext - Fatal编程技术网

Android 从我的活动类访问变量

Android 从我的活动类访问变量,android,variables,global-variables,applicationcontext,Android,Variables,Global Variables,Applicationcontext,我在应用程序类中创建了一些getters setter,如下所示: public class myApp extends Application{ //"Global" variables private Boolean musicEnabled = true; //Music on or off? private Boolean soundEnabled = true; //Sound effects on or off? @Override public v

我在应用程序类中创建了一些getters setter,如下所示:

public class myApp extends Application{

//"Global" variables

private Boolean musicEnabled = true;        //Music on or off?
private Boolean soundEnabled = true;        //Sound effects on or off?

@Override
public void onCreate() {
    // TODO Auto-generated method stub

    musicEnabled = true;            //Default value for music
    soundEnabled = true;            //Default value for sound effects

    super.onCreate();
}

//Getter and setter for musicEnabled

public Boolean getMusicOption(){
    return musicEnabled;                    //Getter


}
public void setMusicOption(Boolean value){  //Setter

    musicEnabled = value;

    }

//Getter and setter for soundEnabled

public Boolean getSoundOption(){            
    return soundEnabled;
}

public void setMusicOptions(Boolean value){
    soundEnabled = value;             
}

}
myApp myAppSettings = (myApp)getApplicationContext();   
musicEnabled = myAppSettings.getMusicOption();
soundEnabled = myAppSettings.getSoundOption();
然后我在我的活动类中获得如下值:

public class myApp extends Application{

//"Global" variables

private Boolean musicEnabled = true;        //Music on or off?
private Boolean soundEnabled = true;        //Sound effects on or off?

@Override
public void onCreate() {
    // TODO Auto-generated method stub

    musicEnabled = true;            //Default value for music
    soundEnabled = true;            //Default value for sound effects

    super.onCreate();
}

//Getter and setter for musicEnabled

public Boolean getMusicOption(){
    return musicEnabled;                    //Getter


}
public void setMusicOption(Boolean value){  //Setter

    musicEnabled = value;

    }

//Getter and setter for soundEnabled

public Boolean getSoundOption(){            
    return soundEnabled;
}

public void setMusicOptions(Boolean value){
    soundEnabled = value;             
}

}
myApp myAppSettings = (myApp)getApplicationContext();   
musicEnabled = myAppSettings.getMusicOption();
soundEnabled = myAppSettings.getSoundOption();
这很好,但我不知道如何从相应的surfaceView类中获取并使用它们?i、 e.开始的课程:

public class mySView extends SurfaceView implements
  SurfaceHolder.Callback {
到目前为止,我唯一能做到这一点的方法是通过创建如下方法将它们传递到我的surfaceview类:

public void initialise(Boolean Sound, Boolean Music){

}
然后从我的活动课上传下来如下:

myView.initialise(musicEnabled, soundEnabled).
这是可行的,但是看起来有点混乱,我的意思是我需要使用我的“myView”类中的setter来设置这些值,以便。。。。。。。。我是否可以直接从我的“myView”类访问它们,还是必须从Activity类访问它们


谢谢大家

您应该能够从自定义的
SurfaceView
内部调用
getContext().getApplicationContext()
,然后像在上面的示例中一样键入它。请参阅。

您可以创建一些方法,例如,
init()
并从mySView的所有构造函数中调用它

在init方法中,您可以执行与“活动”中相同的操作:

private void init() {
    myApp myAppSettings = (myApp)getContext().getApplicationContext();   
    musicEnabled = myAppSettings.getMusicOption();
    soundEnabled = myAppSettings.getSoundOption();
}

另一个选项是将您的设置保存在SharedReferences中,该设置可在应用程序中具有上下文的任何位置访问。 这样,您上次使用应用程序时所做的任何更改都将自动保留,以便在下次启动时加载,而无需在每次启动时将值返回到默认值

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

在我看来,将参数传递给构造函数和类方法比您的方法要简单得多。当不依赖意大利面代码访问随机全局变量时,易于维护、可扩展性和重用性要好得多。既然它们是简单的布尔值,为什么不使用SharedReference而不是整个应用程序类?@Simon,我很清楚,你的意思是像我已经在做的那样将变量/值从我的应用程序类传递到我的surfaceview类(但是去掉应用程序类)?谢谢。@daniel_c05,据我所知,即使我使用了SharedReferences,我仍然需要应用程序类从view/surfaceview类访问上述SharedReferences?每个视图都有一个可用于解析SharedReferences的上下文。在没有任何上下文的情况下,你无法创建任何可见的内容,这是任何应用程序的基本组成部分,可以从中解析资源和其他内容。你应该花点时间阅读文档。太棒了,这正是我想要的:-)谢谢,这是我没有考虑过的东西-我会调查的!