Reputation: 1126
I want to check if I have save data in my shared preferences read the file, because if the file not exist and I do that my app wont work.
I search stack overflow and somebody offered to do this:
File f = new File(
"/data/data/your_application_package/shared_prefs/Name_of_your_preference.xml");
if (f.exists()){
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
}
else
Log.d("TAG", "Setup default preferences");
Now I don't know I use it correctly or not. and this is where I save my pref:
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("text", showcountTv.getText().toString());
editor.apply();
Edited: Of course I know I can :
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
but when I do that at the first time my app installed because I nothing have been saved , my program crashed.This is logcat:
06-19 09:53:25.794 20485-20485/com.pirisalavat.amirhossein.amir D/AndroidRuntime﹕ Shutting down VM
06-19 09:53:25.795 20485-20485/com.pirisalavat.amirhossein.amir E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.pirisalavat.amirhossein.amir, PID: 20485
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pirisalavat.amirhossein.amir/com.pirisalavat.amirhossein.amir.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
at com.pirisalavat.amirhossein.amir.MainActivity.getdataformsharedpref(MainActivity.java:73)
at com.pirisalavat.amirhossein.amir.MainActivity.onCreate(MainActivity.java:30)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Upvotes: 1
Views: 209
Reputation: 1482
Try this:
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
if(prefs.contains("text")) {
//data exists
} else {
//data doesn't exist
}
Upvotes: 0
Reputation: 29416
There is a SharedPreferences
method called contains
which you can use for this purpose:
if (getPreferences(MODE_PRIVATE).contains("text")) {
// do something
}
And I don't recommend creating a File
pointing to the SharedPreferences
- you can't know for sure that this file will be created in the directory called "/data/data/your_application_package/shared_prefs" - there are plenty of vendors and each of them has plenty of devices, so this file can be contained somewhere else/its name will be different/etc. contains()
will be enough.
Upvotes: 4
Reputation: 600
Why don't you just do this:
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if(restoredText == null) {
//you don't have any saved data
} else {
//you do have saved data so do something with it here
}
If restoredText is null then you know that no data has been saved.
Upvotes: 4