0
我正在製作一個android java遊戲。但我有點卡住獲取我保存的性別字符串。製作android遊戲:無法訪問我的SharedPreferences,它擴展了SurfaceView的實現SurfaceHolder.Callback
在我的GameGameActivity類中,我可以收到性別。
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);
}
}
}
但是所有的魔法都發生在我的MainGamePanel中。我想知道那裏是什麼性別。我嘗試了很多方法,但它總是給我一個空指針異常。這是我的MainGamePanel。 (只有幾行)
public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback{
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);
}
這是我保存性別等的類。我可以在其他活動的ACCES到他們,但他們都extentends這個類:
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閱讀中的性別偏好。有人能告訴我還是推動我走向正確的方向?
謝謝
我想它的命令後這樣說: ,它說:該方法getSharedPreferences(字符串,整數)是未定義的類型MainGamePanel
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);
}
但它不知道GameActivity類中的GAME_PREFERENCES_GENDER。如何從我的mainpanel類訪問GAME_PREFERENCES_GENDER。如果我嘗試的方式與我在GameGameActivity中的做法相同,它會給我:方法getSharedPreferences(String,int)未定義爲MainGamePanel類型 – Kim 2013-04-04 14:59:21
哦,我看到我必須將mGameSettings = context.getSharedPreferences(mGameActivity.GAME_PREFERENCES, Context.MODE_PRIVATE);現在它的作品謝謝你:) – Kim 2013-04-04 15:04:53