please help me get sharedpreferences saved value from other class-Collection of common programming errors
I am saving the value in sharedPreferences in one Class and trying to access it from another class but get null pointer exception. the value that i enter is url for web service. please help me.
UrlSetting.java
public class UrlSetting extends Activity {
EditText editUrl;
TextView Savedurl;
Button bSaveUrl, bback;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.urlsetting);
Savedurl = (TextView)findViewById(R.id.savedurl);
editUrl = (EditText)findViewById(R.id.editurl);
bback = (Button)findViewById(R.id.back);
bSaveUrl = (Button)findViewById(R.id.saveurl);
bSaveUrl.setOnClickListener(buttonSaveUrlOnClickListener);
bback.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(UrlSetting.this, Login.class);
/*intent.setClass(UrlSetting.this, Login.class);*/
startActivity(intent);
}});
LoadPreferences();
}
Button.OnClickListener buttonSaveUrlOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SavePreferences("URL", editUrl.getText().toString());
LoadPreferences();
}
private void SavePreferences(String key, String value){
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
private void LoadPreferences(){
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
String strSavedurl = sharedPreferences.getString("URL", "");
Savedurl.setText(strSavedurl);
}
and this is my Koneksi.java
public class Koneksi{
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
String url = sharedPreferences.getString("URL", "");
public Koneksi(String urlml){
url= url + urlml;
}
public String getUrl(){
return url;
}
this is the LogCat
> 05-21 11:39:44.542: W/KeyCharacterMap(339): No keyboard for id 0
05-21 11:39:44.542: W/KeyCharacterMap(339): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-21 11:39:51.826: D/TryLoginCheck(339): Here
05-21 11:39:51.826: I/System.out(339): UserNametes
05-21 11:39:51.831: I/System.out(339): passwordTes12345*
05-21 11:39:51.831: D/Parameters(339): username=tes&password=Tes12345*
05-21 11:39:51.831: D/AndroidRuntime(339): Shutting down VM
05-21 11:39:51.831: W/dalvikvm(339): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-21 11:39:51.851: E/AndroidRuntime(339): FATAL EXCEPTION: main
05-21 11:39:51.851: E/AndroidRuntime(339): java.lang.NullPointerException
05-21 11:39:51.851: E/AndroidRuntime(339): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
05-21 11:39:51.851: E/AndroidRuntime(339): at mobile.config.Koneksi.(Koneksi.java:13)
05-21 11:39:51.851: E/AndroidRuntime(339): at com.karismaelearning.Login.tryLogin(Login.java:131)
05-21 11:39:51.851: E/AndroidRuntime(339): at com.karismaelearning.Login$3.onClick(Login.java:80)
05-21 11:39:51.851: E/AndroidRuntime(339): at android.view.View.performClick(View.java:2485)
05-21 11:39:51.851: E/AndroidRuntime(339): at android.view.View$PerformClick.run(View.java:9080)
05-21 11:39:51.851: E/AndroidRuntime(339): at android.os.Handler.handleCallback(Handler.java:587)
05-21 11:39:51.851: E/AndroidRuntime(339): at android.os.Handler.dispatchMessage(Handler.java:92)
05-21 11:39:51.851: E/AndroidRuntime(339): at android.os.Looper.loop(Looper.java:123)
05-21 11:39:51.851: E/AndroidRuntime(339): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-21 11:39:51.851: E/AndroidRuntime(339): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 11:39:51.851: E/AndroidRuntime(339): at java.lang.reflect.Method.invoke(Method.java:507)
05-21 11:39:51.851: E/AndroidRuntime(339): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-21 11:39:51.851: E/AndroidRuntime(339): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-21 11:39:51.851: E/AndroidRuntime(339): at dalvik.system.NativeStart.main(Native Method)
05-21 11:39:54.301: I/Process(339): Sending signal. PID: 339 SIG: 9
please help me to know what the mistake is. I don’t understand what’s wrong. thank you very much
-
try this way:
SharedPreferences sharedPreferences; onCreate(Bundle savedInstanceState){ sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); // your code.. } private void SavePreferences(String key, String value){ SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(key, value); editor.commit(); } private void LoadPreferences(){ String strSavedurl = sharedPreferences.getString("URL", ""); Savedurl.setText(strSavedurl); }
Edited:
public class Koneksi{ SharedPreferences sharedPreferences; Context myC; String url = ""; public Koneksi(Context con){ myC = con; sharedPreferences = PreferenceManager.getDefaultSharedPreferences(con); } public Koneksi(String urlml){ url= url + urlml; } public String getUrl(){ String url = sharedPreferences.getString("URL", ""); return url; } }
for call above class from activity:
Koneksi ob = new Koneksi(this); String URL = ob.getUrl();
-
try these two methods :p works well
public static void SavePreferences(String key, String name, String value,Context c) { SharedPreferences sf = c.getSharedPreferences(key, 0); SharedPreferences.Editor editor = sf.edit(); editor.putString(name, value); editor.commit(); Log.w("Preference saved .", value); } public static String LoadPreferences(String key, String name, Context c) { SharedPreferences sf = c.getSharedPreferences(key, 0); String val = null; val = sf.getString(name, ""); return val; }
-
- you should be pass the context mContext.getSharedPreferences(PREF_NAME, MODE_PRIVATE); other wise use following code :- import android.preference.PreferenceManager; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); // then you use prefs.getBoolean("keystring", true);
-
These will give you
NPE
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE); String url = sharedPreferences.getString("URL", "");
Move the initializations to
onCreate()
which I don’t see but assuming you have since you are extendingActivity
public class Koneksi extends Activity { SharedPreferences sharedPreferences; //declare them here String url; public void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.your_layout); sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE); // initialize them here url = sharedPreferences.getString("URL", ""); // intialize here public Koneksi(String urlml){ url= url + urlml; }
SharedPreferences
needs aContext
which isn’t available until afteronCreate()
is called. You can declare them before this as you are but you need to initialize them inonCreate()
or anotherActivity
method afteronCreate()
is called -
i dont know what is the problem with your code, i have working implementation for same..
public class GreetingCardData { public static final String SHARED_PREF_FILE = "greetingCardData"; public static final String KEY_DO_NOT_SHOW = "doNotShow"; public static final String KEY_CATEGORIES_JSON = "categoriesJson"; private SharedPreferences sharedPrefs; private Editor prefsEditor; public GreetingCardData(Context context) { this.sharedPrefs = context.getSharedPreferences(SHARED_PREF_FILE, 0); this.prefsEditor = sharedPrefs.edit(); } public void setDoNotShowFlag ( boolean flag ){ prefsEditor.putBoolean( KEY_DO_NOT_SHOW, flag ); prefsEditor.commit(); } public boolean getDoNotShowFlag(){ return sharedPrefs.getBoolean( KEY_DO_NOT_SHOW, false ); } public void setGreetingcardJson( String jsonString ){ prefsEditor.putString( KEY_CATEGORIES_JSON, jsonString ); prefsEditor.commit(); } public String getGreetingcardJsonString(){ return sharedPrefs.getString(KEY_CATEGORIES_JSON, ""); } }
and you can access your pref. anywhere by just calling it like..
if( !new GreetingCardData(ActivityMain.this).getDoNotShowFlag()) startHomeActivity(); new GreetingCardData(context).setGreetingcardJson(resBuffer.toString());
Originally posted 2013-11-16 20:50:26.