在后台登录时,解析器抛出了一个空指针异常

3

我有一个登录方法如下:

private void login() {
    String username = this.email.getText().toString();
    String password = this.password.getText().toString();

    ParseUser.logInInBackground(username, password, new LogInCallback() {
        @Override
        public void done(ParseUser user, ParseException e) {
            if (user != null) {
                // Successful login. Redirect to the main screen.
                startActivity(mainView);
            } else {
                // We failed. Notify the user and log.
                message.setText("Error logging in. Please try again later.");
                Log.e("Error", "Error logging in user", e);
            }
        }
    });
}

看起来没问题,但是当我尝试登录时,我收到一个NullPointerException的错误提示:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.parse.ParseHttpClient com.parse.ParsePlugins.restClient()' on a null object reference

一开始,我觉得是Parse的问题,但现在我在想是否有什么地方我没注意到。我是通过以下方式将Parse添加到我的Gradle依赖项中的:

compile 'com.parse.bolts:bolts-android:1.+'
compile 'com.parse:parse-android:1.10.3'

我声明依赖关系的方式是否有误?这是解析器中的错误,还是我使用该方法不正确。

我看不到你在哪里调用 restClient - Andy Turner
2
你调用了 Parse.initialize 吗? - Blackbelt
在我的应用程序类中。 - James Parsons
1
请发布您的完整堆栈跟踪。 - njzk2
哪一行代码导致了 NPE? - Code-Apprentice
4个回答

3
你在清单文件中更改了应用程序吗?
记住当你做所有这些操作时。
 <application
    android:allowBackup="true"
    android:name=".android.ParseApplication"

这是非常重要的!我只是忘了它


1

我曾经遇到过完全相同的问题,但是我解决了它。可能你缺少以下两点之一:

  1. 你需要创建一个继承自 Application 的类,并在其中添加 Parse.Initialize(..),具体请参考这里
  2. 别忘了在 AndroidManifest.xml 文件中声明这个类:


1
确保您在OnCreate()方法中初始化parse。
    Parse.enableLocalDatastore(this);
    Parse.initialize(this, "AppID", "ClientID");

0
如果您正在使用Parse开源服务器并遇到此问题,则这将对您有所帮助。
我是这样初始化我的Parse的:
                Parse.initialize(new Parse.Configuration.Builder(MainApplication.this)
                    .applicationId("my_super_secret_id")
                    .clientKey(null)
                    .server("https://mysupersecretpage.com/parse/").build();

所以解决问题的方法是加上 .clientKey("")。

这只是一个解决方法,但看起来sdk中存在一个bug。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接