应用在不同设备上崩溃 [英] App Crahes in different devices

查看:96
本文介绍了应用在不同设备上崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So, My app crashes on 3 other devices but it works fine on the device I have been using since the beginning of the development process. When I checked the LogCat, I got this error :

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

我认为是因为以下代码:

I assume it is because of this code:

    //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);
    }

但是,这怎么可能是个问题?我已经使用第一台设备反复登录和退出应用程序,没有任何问题。为什么会导致其他设备出现问题?

But, how can this be a problem? I have been logging in and out of the app with the first device over and over again without any problem. And why is it causing problem with the other devices?

更新:

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

I just found out if I remove the code, the app works just fine. But it doesn't work when I have the code.

推荐答案

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

You can avoid the crash by remove the problematic line from outside the Null checking and insert it to the if loop::

//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
} 

这篇关于应用在不同设备上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆