2012-06-06 3 views
0

방금 ​​실수로 마지막 질문을 삭제할 수있게되었습니다. 다시 여기 있습니다.다른 클래스의 Android 환경 설정

나는 환경 설정 클래스 테스트를 위해 아래의 클래스를 사용했다. 내가 직면하고있는 문제는 환경 설정 클래스에서 전달하거나 호출 할 때 아무 일도 일어나지 않는다는 것입니다. 코드는 잘 컴파일되지만 기본 설정을 가져 오거나 설정할 수 없습니다. SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);에 멈추는 것처럼 보입니다. 일부 인쇄 문을 사용하여 멈추는 위치를 확인했습니다. 나는 안드로이드 사이트에서 예제를 사용하여 시도했지만 도움이되지 않습니다. 누구든지 아이디어가있어?

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; 
} 
} 
+0

시도 게시. 당신은 아마도 그것을 되돌릴 수있는 모드를 얻을 수 있습니다. – gobernador

+0

이전 질문에서 물었던 것처럼 getDefaultSharedPreferences (this)를 사용하지 않는 이유가 있습니까? – Tushar

+0

죄송합니다. 삭제하지 않으 셨습니다. 그냥 그들을 읽고. 당신이 간단한 예제를 얻는다면 나는 감사 할 것입니다 :) – James

답변

0

난 항상 아래와 같이 호출하여 기본 설정을 검색 : 메타의

String location = PreferenceManager.getDefaultSharedPreferences(this).getString("location", "");