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

后端 未结 5 2126
甜味超标
甜味超标 2020-12-07 00:50

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

public void onCreate() {
-->here Parse.initialize(this, \"adfsfasdfs\",
            \"asdfadfsdf\"         


        
相关标签:
5条回答
  • 2020-12-07 01:21

    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>
    
    0 讨论(0)
  • 2020-12-07 01:23

    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();
    
        }
    }
    
    0 讨论(0)
  • 2020-12-07 01:29

    I solved it using a boolean isParseInitialized variable .Works on orientation change i.e. 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;
        }
    
      ..........
    
    }
    
    0 讨论(0)
  • 2020-12-07 01:36

    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();
    }
    
    0 讨论(0)
  • 2020-12-07 01:39

    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();
            }
    
    0 讨论(0)
提交回复
热议问题