ParseTwitterUtils初始化抛出NullPointerException

时间:2015-09-30 00:34:21

标签: android twitter parse-platform

我从调用

获得NullPointerException
ParseTwitterUtils.initialize(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);

这是堆栈跟踪

java.lang.RuntimeException: Unable to create application com.myapp.app.android.Application: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5015)
            at android.app.ActivityThread.access$1600(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference
            at com.parse.Parse.getParseDir(Parse.java:313)
            at com.parse.ParseCorePlugins.getCurrentUserController(ParseCorePlugins.java:124)
            at com.parse.ParseUser.getCurrentUserController(ParseUser.java:56)
            at com.parse.ParseUser.getCurrentUser(ParseUser.java:892)
            at com.parse.ParseUser.getCurrentUser(ParseUser.java:879)
            at com.parse.ParseUser.registerAuthenticationProvider(ParseUser.java:1125)
            at com.parse.ParseTwitterUtils.getAuthenticationProvider(ParseTwitterUtils.java:20)
            at com.parse.ParseTwitterUtils.initialize(ParseTwitterUtils.java:50)
            at com.myapp.app.android.Application.onCreate(Application.java:42)
            at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1020)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5012)
            at android.app.ActivityThread.access$1600(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

在添加第ParseTwitterUtils.initialize(...)行之前,我的应用程序曾经工作过。这是我的Application类的onCreate方法

public void onCreate(){
    super.onCreate();
    registerDatabaseTables();
    ParseTwitterUtils.initialize(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
    Parse.enableLocalDatastore(this);
    Parse.initialize(this, PARSE_APPLICATION_ID, PARSE_CLIENT_KEY);
}

1 个答案:

答案 0 :(得分:0)

不幸的是,parse.com上的说明并未说明这一点,但您必须在初始化Twitter之前初始化Parse。这并不明显,因为在初始化解析之前需要做很多事情。所以无论如何将代码更改为:

public void onCreate(){
    super.onCreate();
    registerDatabaseTables();
    Parse.enableLocalDatastore(this);
    Parse.initialize(this, PARSE_APPLICATION_ID, PARSE_CLIENT_KEY);
    ParseTwitterUtils.initialize(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
}