简体   繁体   中英

Caused by: java.lang.IllegalStateException: ParsePlugins is already initialized

I quit the app, relaunch it, I am getting an exception.

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

Exception

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)

Manifest file

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

Edit :

I wonder why Parse would throw an exception for this. Why not just info and move on. Its initialized, so big deal if I initialized it again.

Solution

I have given up on Parse. Dont like the Application way, just to irritating to maintain.

Parse.initialize() should only be called once for an entire application .

Calling it in an Activity 's onCreate function can cause it to be initialized more than once, as an Activity can be created more than once during an app's lifecycle .

Instead, create an Application class (and add an android:name attribute to your your application's manifest).

Application: (Note not an Activity/Service/Reciever)

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

AndroidManifest:

<application
        android:name=".MyApplication">
        ....
        <activity>
            ....
        </activity>
</application>

NEVERMIND, I've fixed it. The problem was due to a syntax error. Thanks to all for solution.

This is weird as I've followed what's given but now I'm not getting any push notifications at all? The only changes I've made:

  1. add app class to the manifest &
  2. initialize parse in the app class. I'm using v1.10.1 of the SDK...

Manifest

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

Application class

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

Check for initialization yourself and just handle the exception and the error will not crash the app just quietly throw the exception.

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

I solved it using a boolean isParseInitialized variable .Works on orientation change ie when activity is recreated in the same Application session . Code snippet :

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

  ..........

}

this is a solution

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();

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM