0

前回の質問を誤って削除してしまったので、もう一度。

設定クラスをテストするためにまとめた以下のクラスがあります。私が直面している問題は、設定クラスから渡したり呼び出したりしても何も起こらないことです。コードは正常にコンパイルされますが、設定を取得または設定できません。SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);いくつかの印刷ステートメントを使用してどこで停止するかを確認したため、行き詰まっているようです。Android サイトの例を使用してみましたが、役に立ちません。誰でもアイデアはありますか?

public class Preferences extends Activity{

public void getSetPrefs(){

    Preferences p = new Preferences();

    p.setLocationPref("london", "1");

    String location = p.getLocation();
    String location_id = p.getLocation();

}
}

//設定クラス

public class Preferences extends Activity{

    /** Sets location preferences */
public void setLocationPref(String location, int location_id){

    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("location", location);
    editor.putInt("location_id", location_id);
    editor.commit();
}

/** Get location from preferences */
public String getLocation(){

    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    String location = sharedPreferences.getString("location", "");

    return location;
}

/** Get location ID from preferences */
public int getLocationID(){

    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    int locationID = sharedPreferences.getInt("location_id", 0);

    return locationID;
}
}
4

1 に答える 1

0

私はいつも以下のような呼び出しで設定を取得します:

String location = PreferenceManager.getDefaultSharedPreferences(this).getString("location", "");
于 2012-06-06T20:21:26.490 に答える