引起:java.lang.IllegalStateException:ParsePlugins已经初始化

时间:2015-07-08 18:02:29

标签: java android parse-platform

我退出了应用程序,重新启动它,我得到了例外。

public void onCreate() {
-->here Parse.initialize(this, "adfsfasdfs",
            "asdfadfsdf");
    ParseInstallation.getCurrentInstallation().saveInBackground();
    ParseInstallation.create(identity == null ? "No Identity Set"
            : identity);

异常

07-08 23:27:29.411: E/AndroidRuntime(4889): Caused by: java.lang.IllegalStateException: ParsePlugins is already initialized
07-08 23:27:29.411: E/AndroidRuntime(4889):     at com.parse.ParsePlugins.set(ParsePlugins.java:27)
07-08 23:27:29.411: E/AndroidRuntime(4889):     at com.parse.ParsePlugins.access$200(ParsePlugins.java:11)
07-08 23:27:29.411: E/AndroidRuntime(4889):     at com.parse.ParsePlugins$Android.initialize(ParsePlugins.java:141)
07-08 23:27:29.411: E/AndroidRuntime(4889):     at com.parse.Parse.initialize(Parse.java:178)
07-08 23:27:29.411: E/AndroidRuntime(4889):     at com.mcruiseon.caregiri.Registration.onCreate(Registration.java:98)

清单文件

        <service android:name="com.parse.PushService" />

        <receiver android:name="com.parse.ParseBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
        <receiver
            android:name="com.parse.ParsePushBroadcastReceiver"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.parse.push.intent.RECEIVE" />
                <action android:name="com.parse.push.intent.DELETE" />
                <action android:name="com.parse.push.intent.OPEN" />
            </intent-filter>
        </receiver>

编辑:

我想知道为什么Parse会为此抛出异常。为什么不只是info继续前进。它的初始化,如果我再次初始化它是如此重要。

解决方案

我放弃了Parse。不喜欢应用方式,只是为了刺激维护。

5 个答案:

答案 0 :(得分:28)

Parse.initialize()只应为整个应用程序调用 一次

Activity的{​​{1}}函数中调用它可以使其多次初始化,因为可以在应用的生命周期内多次创建一个Activity

相反,请创建应用程序类(并在应用程序的清单中添加onCreate属性)。

申请:(注意不是活动/服务/接收者)

android:name

<强> AndroidManifest:

//Note that this is an android.app.Application class.
public class MyApplication extends android.app.Application {

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

    //This will only be called once in your app's entire lifecycle.
    Parse.initialize(this,
            getResources().getString(R.string.parse_application_id),
            getResources().getString(R.string.parse_client_key));
}

答案 1 :(得分:4)

NEVERMIND,我已经修好了。问题是由于语法错误。感谢大家的解决方案。

这很奇怪,因为我已经按照给出了什么,但现在我根本没有得到任何推送通知?我所做的唯一改变是:

  1. 将应用类添加到清单&amp;
  2. 在app类中初始化解析。我正在使用SDK的v1.10.1 ......
  3. 清单

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        android:name="full.package.name.UseParse" >
    

    申请类

    public class UseParse extends android.app.Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Parse.initialize(this, "id", "key");
        ParseInstallation.getCurrentInstallation().saveInBackground();
    }
    

答案 2 :(得分:2)

自己检查初始化并只处理异常,错误不会导致应用程序崩溃,只是悄悄地抛出异常。

        try {
            Parse.initialize(this);
            parseinited = true;
        }
        catch (IllegalStateException e) {
            e.printStackTrace();
        }

答案 3 :(得分:1)

我使用boolean isParseInitialized 变量.Works解决方向更改,即在同一个应用程序会话中重新创建活动时。代码段:

   public class YourActivity extends Activity {
        private static boolean isParseInitialized = false;
        public static final String APPLICATION_ID = "your_application_id";
        public static final String CLIENT_KEY = "your_client_key";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_youractivity);


    if(isParseInitialized==false) {
        Parse.initialize(this, APPLICATION_ID, CLIENT_KEY);
        isParseInitialized = true;
    }

  ..........

}

答案 4 :(得分:0)

这是一个解决方案

public class MainActivity extends AppCompatActivity {
    static  boolean  parseIsInit = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (!parseIsInit){
            Log.d("demo",""+parseIsInit );
        Parse.initialize(this, "PutHereYourKeys", "PutHereYourKeys");
            parseIsInit=true;
        }
        ParseInstallation.getCurrentInstallation().saveInBackground();

    }
}