마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

Class에서 SharedPreferences 값 불러오기 에러

0 추천
메인액티비티에서 SharedPreferences에 위도, 경도 값을 저장하구요.
Class에서 그 값을 불러오기할 때 getSharedPreferences에서 계속 빨간줄이 떠서
Context를 이용해 돌려봤습니다. 코딩상 문제는 없는데
계속 파일을 못불러 오네요. 문제가 뭘까요? 
어떻게 해야하나요ㅠ 
 
public class MainActivity extends Activity {
 
private SharedPreferences prvPrefLocation;
    private SharedPreferences.Editor editorLat;
    private SharedPreferences.Editor editorLon;
 
prvPrefLocation = getSharedPreferences("Location_pf", Context.MODE_PRIVATE);
                editorLat = prvPrefLocation.edit();
                editorLon = prvPrefLocation.edit();
                
                editorLat.putString("lat", Double.toString(Lat));
                editorLon.putString("lon", Double.toString(Lon));
                editorLon.commit();
                editorLon.commit();
 
 
 
////값 불러오는 클래스 
public class GmailClient {
 
private Context mContext=null; // private Context mContext; null값 없이도 선언해서 돌려봄 
private SharedPreferences prvPrefLocation;
double Dlat;
double Dlon;
 
public void setAccountDetails(String userName, String password){
this.userName = userName;
this.password = password;
}
 
public List<String> readGmail() throws MessagingException{
receivingHost="imap.gmail.com";
Properties props=System.getProperties();
props.setProperty("mail.store.protocol","imaps");
Session session = Session.getDefaultInstance(props);
 
prvPrefLocation = mContext.getSharedPreferences("Location_pf", Context.MODE_PRIVATE);
String slat= prvPrefLocation.getString("lat", "");
String slon= prvPrefLocation.getString("lon", "");
 
this.Dlat = Double.parseDouble(slat);
this.Dlon = Double.parseDouble(slon);
익명사용자 님이 2014년 6월 4일 질문

1개의 답변

0 추천
이거 그냥 코드 복사해 온거 맞나요?
그리고 mContext가 null값이면 당연히 함수가 제기능을 할 수 없습니다.
GmailClient클래스는 사용하는 코드가 없네요...
다른 클래스에서 Context를 사용하시려면 클래스를 호출할때 Context도 넘겨줘야 합니다
 
public class MainActivity extends Activity {
 
private SharedPreferences prvPrefLocation;
    private SharedPreferences.Editor editorLat;
    private SharedPreferences.Editor editorLon;
 
prvPrefLocation = getSharedPreferences("Location_pf", Context.MODE_PRIVATE);
                editorLat = prvPrefLocation.edit();
                editorLon = prvPrefLocation.edit();
                
                editorLat.putString("lat", Double.toString(Lat));
                editorLon.putString("lon", Double.toString(Lon));
                editorLon.commit();
                editorLon.commit(); // 클래스안은 메소드안이 아닙니다 선언이 아니면 onCreate() 와 같은 메소드 안에 있어야 합니다
 
 
 
////값 불러오는 클래스 
public class GmailClient {
 
private Context mContext=null; // private Context mContext; null값 없이도 선언해서 돌려봄 
private SharedPreferences prvPrefLocation;
double Dlat;
double Dlon;
 
public void setAccountDetails(String userName, String password){
this.userName = userName;
this.password = password;
}
 
public List<String> readGmail() throws MessagingException{
receivingHost="imap.gmail.com";
Properties props=System.getProperties();
props.setProperty("mail.store.protocol","imaps");
Session session = Session.getDefaultInstance(props);
 
prvPrefLocation = mContext.getSharedPreferences("Location_pf", Context.MODE_PRIVATE);
String slat= prvPrefLocation.getString("lat", "");
String slon= prvPrefLocation.getString("lon", "");
 
this.Dlat = Double.parseDouble(slat);
this.Dlon = Double.parseDouble(slon);
루빈스 (620 포인트) 님이 2014년 6월 4일 답변
...