我正在尝试将对象保存在内部存储中,并且可以正常工作,我可以保存数据,检索数据,但是在官方站点上说,仅当您卸载应用程序时,才会删除存储在内部存储中的文件,因此,如果我退出应用程序,然后重新启动它,它应该在该位置,但是当我尝试检索数据时遇到null object reference exception
时,我可能做错了。任何人都可以让我回到正确的轨道上。
public class UserData implements Serializable {
public static User user;
static UserData instance=null;
public static UserData getInstance(){
if( instance == null )
instance = new UserData();
return instance;
}
public static void setUser(User usr){
user = new User(usr.getNume(),usr.getPhone(),usr.getEmail());
}
public static void saveData(UserData instance,Context ctx,User usr){
setUser(usr);
String filename = "User.data";
ObjectOutput out;
try {
File outFile = new File(ctx.getFilesDir(), filename);
out = new ObjectOutputStream(new FileOutputStream(outFile));
out.writeObject(instance);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static UserData loadData(Context ctx){
ObjectInput in;
String filename = "User.data";
UserData ss=null;
try {
File outFile = new File(ctx.getFilesDir(), filename);
in = new ObjectInputStream(new FileInputStream(outFile));
ss=(UserData) in.readObject();
in.close();
} catch (Exception e) {e.printStackTrace();}
return ss;
}
}
这是User类的实现方式
public class User {
private String nume;
private String phone;
private String email;
public User(String nume, String phone, String email) {
this.nume = nume;
this.phone = phone;
this.email = email;
}
public String getNume() {
return nume;
}
public String getPhone() {
return phone;
}
public String getEmail() {
return email;
}
}
下面是检索数据的调用,但是包含params的对象是null
UserData user = UserData.getInstance();
user.loadData(Login.this);
if(user.user != null){ // commenting this line will lead to the inner if and accesing user.user.getNume() will throw a null pointer exception
if(user.user.getNume() != "null"){
Log.d("STATE", user.user.toString());
Intent intent = new Intent(Login.this,MenuRest.class);
this.startActivity(intent);
}
}
编辑,如下所示是错误的堆栈跟踪:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: gabriel.androidf, PID: 30318
java.lang.RuntimeException: Unable to start activity ComponentInfo{gabriel.androidf/gabriel.androidf.activities.Login}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String gabriel.androidf.models.User.getNume()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String gabriel.androidf.models.User.getNume()' on a null object reference
at gabriel.androidf.activities.Login.onCreate(Login.java:42)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Application terminated.
答案 0 :(得分:0)
使用此代码即可:
public class UserData implements Serializable {
public User user;
static UserData instance=null;
private static File myfolder;
public static UserData getInstance(){
if( instance == null )
instance = new UserData();
return instance;
}
public void setUser(User usr){
user = new User(usr.getNume(),usr.getPhone(),usr.getEmail());
}
public User getUser(){
return user;
}
public void saveData(Activity pContext){
if(myfolder == null){
myfolder = pContext.getExternalFilesDir(null);
}
ObjectOutput out;
try {
File outFile = new File(myfolder,
"someRandom.data");
out = new ObjectOutputStream(new FileOutputStream(outFile));
out.writeObject(instance);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public UserData loadData(Context pContext){
ObjectInput in;
if(myfolder == null){
myfolder = pContext.getExternalFilesDir(null);
}
instance = null;
try {
FileInputStream fileIn = new FileInputStream(myfolder.getPath() + File.separator + "someRandom.data");
in = new ObjectInputStream(fileIn);
instance = (UserData) in.readObject();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return instance;
}
}
以及通过使User类实现可序列化来进行设置