0

私はAndroid Javaゲームを作っています。しかし、保存した性別文字列を取得するのに少し行き詰まっています。

私の 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 で発生します。そこにある性別を知りたいです。私は多くの方法で試しましたが、常に nullpointer 例外が発生します。これは私の 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); 
}

これは、性別などを保存したクラスです。私は他のアクティビティでそれらにアクセスできますが、それらはすべてこのクラスを拡張します:

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();

}

メインゲームパネルの性別設定の読み方がわかりません。誰かが私に教えてくれるか、私を正しい方向に押し進めることができますか?

ありがとうございました

コマンドの後にこの方法で試してみました:そしてそれは言った:メソッドgetSharedPreferences(String、int)はタイプ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);
}
4

1 に答える 1