gpt4 book ai didi

android - 应用程序在不同设备上崩溃

转载 作者:行者123 更新时间:2023-12-02 05:49:39 25 4
gpt4 key购买 nike

所以,我的应用程序在其他 3 台设备上崩溃了,但在我从开发过程开始以来一直使用的设备上运行良好。当我检查 LogCat 时,我收到此错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.......MainActivity}:

java.lang.NullPointerException: Attempt to invoke virtual method

java.lang.String
com.parse.ParseUser.getUsername() on a null object reference

我认为这是因为这段代码:

    //get current user
ParseUser currentUser = ParseUser.getCurrentUser();

//get current user username and turn it to string
final String currentUserUsername = currentUser.getUsername();

//identify if current user is logged in
if (currentUser != null) {
// bring user to homepage and do stuff with the user
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("My");
query.orderByDescending("createdAt");
query.whereEqualTo("user", currentUserUsername);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> personObject, ParseException e) {
if(e == null){
//success
Toast.makeText(MainActivity.this, "Welcome Back, " + currentUserUsername + "!", Toast.LENGTH_SHORT).show();

mPerson = peopleObject;

MyPeopleAdapter adapter = new MyPeopleAdapter(getListView().getContext(), mPerson);
setListAdapter(adapter);
} else {
//problem
Toast.makeText(MainActivity.this, "There is a problem. Please try again later.", Toast.LENGTH_SHORT).show();
}
}
});
} else {
// show the signup or login screen
Intent SignIn = new Intent(this, SignInActivity.class);
startActivity(SignIn);
}

但是,这怎么会成为问题呢?我已经用第一台设备一遍又一遍地登录和退出应用程序,没有任何问题。为什么它会导致其他设备出现问题?

更新:

我刚刚发现如果删除代码,该应用程序就可以正常工作。但当我有代码时它不起作用。

最佳答案

您可以通过从 Null 检查之外删除有问题的行并将其插入 if 循环来避免崩溃::

//identify if current user is logged in
if (currentUser != null) {
//get current user username and turn it to string
final String currentUserUsername = currentUser.getUsername(); //this line was outside earlier
//rest of code
}

关于android - 应用程序在不同设备上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31366061/

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