gpt4 book ai didi

java - 普通类偶尔会返回空值

转载 作者:行者123 更新时间:2023-12-02 01:07:15 26 4
gpt4 key购买 nike

我目前正在编写一个函数,允许用户查看他或她的评分。我遇到的问题是,偶尔,该函数会完美运行,而其他时候,应用程序会崩溃,表明我有空指针异常。问题出在我的代码行

 txtStars = (TextView)findViewById(R.id.rating);
txtStars.setText(Common.currentUser.getRates());

特别是在 getRates() 函数上。这个函数来 self 的 User 类,它是

public class User {

private String email, password, name, phone,rates;
public User (){}

public User(String email, String password, String name, String phone, String rates){
this.email = email;
this.password = password;
this.name = name;
this.phone = phone;
this.rates = rates;
}

public String getRates() {
return rates;
}

public void setRates(String rates) {
this.rates = rates;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}
}

如果需要一些额外的帮助,这是我的 logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usub/com.example.usub.DriverProfile}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.usub.Model.User.getRates()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.usub.Model.User.getRates()' on a null object reference
at com.example.usub.DriverProfile.onCreate(DriverProfile.java:56)
at android.app.Activity.performCreate(Activity.java:7825)
at android.app.Activity.performCreate(Activity.java:7814)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

谢谢

编辑:答案我注意到,currentUser 值仅在登录时建立。因此,由于该值从未重置,数据很快就会被忘记。该应用程序具有“记住我”功能,该功能将绕过 currentUser 的第一次初始化。为了解决这个问题,我在每个需要 currentUser 的 Activity 中添加了下面的代码

 FirebaseDatabase.getInstance().getReference(Common.user_driver_tbl)
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Common.currentUser = dataSnapshot.getValue(User.class);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

最佳答案

看起来currentUserCommon类的静态字段。也就是说,您的代码看起来像这样:

public class Common {

public static User currentUser;

//...
}

您可能会在某个时刻初始化currentUser。但是,如果您的应用程序的进程被终止(用户导航离开并且系统想要回收资源),则该值将丢失。当您的应用进程重新启动时,currentUser 不会自动恢复...因此它为 null

您必须 (a) 找出一种方法来在进程死亡时保存 currentUser (也许您可以将其作为某些 Activity 实例状态的一部分并使用 onSaveInstanceState() code> 来保存它)或 (b) 找出一种在进程死亡后恢复时重新初始化 currentUser 的方法。

在此处了解更多信息:https://developer.android.com/topic/libraries/architecture/saving-states

关于java - 普通类偶尔会返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59851619/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com