解析错误:解析#enableLocalDatastore(Context)`必须在`Parse #initialize(Context)之前调用

时间:2015-05-09 04:04:53

标签: java android parse-platform

所以我按照Parse SDK网站的快速入门指南中的说明进行操作。该应用程序第一次正常运行。但是,当我最小化应用程序并从任务切换器再次运行它时,它会强制关闭。

这个错误对我没有任何意义。 Logcat -

05-09 08:57:40.611  19419-19419/com.example.shubhamkanodia.bookmybook E/CrashReporting﹕ ParseCrashReporting caught a RuntimeException exception for com.example.shubhamkanodia.bookmybook. Building report.
05-09 08:57:40.626  19419-19419/com.example.shubhamkanodia.bookmybook E/CrashReporting﹕ Handling exception for crash
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shubhamkanodia.bookmybook/com.example.shubhamkanodia.bookmybook.MainActivity}: java.lang.IllegalStateException: `Parse#enableLocalDatastore(Context)` must be invoked before `Parse#initialize(Context)`
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3947)
            at android.app.ActivityThread.access$900(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.IllegalStateException: `Parse#enableLocalDatastore(Context)` must be invoked before `Parse#initialize(Context)`
            at com.parse.Parse.enableLocalDatastore(Parse.java:104)
            at com.example.shubhamkanodia.bookmybook.MainActivity.onCreate(MainActivity.java:21)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3947)
            at android.app.ActivityThread.access$900(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Mainactivity.java

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(!ParseCrashReporting.isCrashReportingEnabled())
            ParseCrashReporting.enable(this);

        Parse.enableLocalDatastore(this); //Its already before initialize
        Parse.initialize(this, "XXX", "XXX");
        ParseInstallation.getCurrentInstallation().saveInBackground();
        ParseAnalytics.trackAppOpenedInBackground(getIntent());
    }

2 个答案:

答案 0 :(得分:23)

你可以有一个单独的类来执行这个初始化的东西。因此,请创建一个扩展ParseApplication

的课程Application
public class ParseApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        ParseCrashReporting.enable(this);
        Parse.enableLocalDatastore(this);
        Parse.initialize(this, "xxx", "xxx");
    }
  }

在AndroidManifest.xml中,将ParseApplication类添加到Application

 <application
        android:name="com.example.parsetry.ParseApplication" // you should replace this based on your package
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

答案 1 :(得分:0)

我在github找到了一个很好的解决方案。 把这段代码改为

Parse.initialize(新Parse.Configuration.Builder(本).applicationId(&#34; dpynb48oUmEVzZXfU5XtUkoFOQcHyR8wKZcdQETK&#34;)clientKey(&#34; lhAqNjCKxTXlVhYjWUX25oOFRaNfGtLD97LFFO5x&#34;)服务器(&#34; {{3 &#34;)。enableLocalDataStore()。build());