不同设备中的App Crahes

时间:2015-07-12 08:54:44

标签: android crash

所以,我的应用程序在其他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);
    }

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

更新

我刚刚发现如果删除代码,该应用程序运行正常。但是当我有代码时它不起作用。

1 个答案:

答案 0 :(得分:0)

您可以通过从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
}