Java 制作android游戏:无法在扩展SurfaceView实现SurfaceHolder.Callback的类中访问我的SharedReferences
我正在制作一个android java游戏。但我有点被困在获取我保存的性别字符串上 在我的GameActivity类中,我可以接收性别Java 制作android游戏:无法在扩展SurfaceView实现SurfaceHolder.Callback的类中访问我的SharedReferences,java,android,Java,Android,我正在制作一个android java游戏。但我有点被困在获取我保存的性别字符串上 在我的GameActivity类中,我可以接收性别 public class GameGameActivity extends GameActivity{ SharedPreferences mGameSettings; public String mGender; MainGamePanel mGamePanel; protected void onCreate(Bundle savedInstanceSt
public class GameGameActivity extends GameActivity{
SharedPreferences mGameSettings;
public String mGender;
MainGamePanel mGamePanel;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
mGameSettings = getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE);
Log.d("myTag", "View added");
checkGender();
}
private void checkGender(){
if (mGameSettings.contains(GAME_PREFERENCES_GENDER)) {
if(mGameSettings.getInt(GAME_PREFERENCES_GENDER, 0) == 2){
mGender = "Female";
Log.d("myTag","gender: " +mGender );
}else{
mGender = "Male";
Log.d("myTag","gender: " +mGender );
}
}
}
但所有的魔法都发生在我的主游戏面板上。我想知道里面是什么性别。我尝试了很多方法,但它总是给我一个空指针异常。这是我的主游戏面板。(只有几行)
这是我保存性别ect的班级。我可以在其他活动中访问它们,但它们都属于本课程:
import android.app.Activity;
public class GameActivity extends Activity{
public static final String GAME_PREFERENCES = "GamePrefs";
public static final String GAME_PREFERENCES_NICKNAME = "Nickname"; // String
public static final String GAME_PREFERENCES_EMAIL = "Email"; // String
public static final String GAME_PREFERENCES_PASSWORD = "Password"; // String
public static final String GAME_PREFERENCES_DOB = "DOB"; // Long
public static final String GAME_PREFERENCES_GENDER = "Gender"; //
public static final String GAME_PREFERENCES_SCORE = "80"; // int
public static final String DEBUG_TAG = "Activity Log";
public static final String TAG = GameActivity.class.getSimpleName();
}
我不知道如何在我的主游戏面板中阅读性别偏好。有人能告诉我或者把我推向正确的方向吗
多谢各位
命令发出后,我试着这样做:
它说:对于MainGamePanel类型,getSharedReferences(String,int)方法是未定义的
SharedPreferences mGameSettings;
public MainGamePanel(Context context, AttributeSet attributeSet){
super(context, attributeSet);
background = BitmapFactory.decodeResource(getResources(),R.drawable.bg_game_boy);
getHolder().addCallback(this);
createContent();
thread = new MainThread(getHolder(), this);
setFocusable(true);
mGameSettings = getSharedPreferences(mGameActivity.GAME_PREFERENCES, Context.MODE_PRIVATE);
}
也许我在您的描述中遗漏了一些内容,但应该与主游戏面板中的context.getSharedReferences()一样简单。但它不知道GameActivity类中我的游戏偏好。如何从我的主面板类访问游戏\ U首选项\性别。如果我像在GameActivity中一样尝试它,它会给我带来:对于MainGamePanel类型,getSharedReferences(String,int)方法是未定义的。哦,我知道我必须将mGameSettings=context.getSharedReferences(mGameActivity.GAME_首选项,context.MODE_PRIVATE)放在其中;现在它工作了,谢谢:)
SharedPreferences mGameSettings;
public MainGamePanel(Context context, AttributeSet attributeSet){
super(context, attributeSet);
background = BitmapFactory.decodeResource(getResources(),R.drawable.bg_game_boy);
getHolder().addCallback(this);
createContent();
thread = new MainThread(getHolder(), this);
setFocusable(true);
mGameSettings = getSharedPreferences(mGameActivity.GAME_PREFERENCES, Context.MODE_PRIVATE);
}