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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31300277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:24:21  来源:igfitidea点击:

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

javaandroidparse-platform

提问by Siddharth

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.access0(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 infoand move on. Its initialized, so big deal if I initialized it again.

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

Solution

解决方案

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

我已经放弃了 Parse。不喜欢Application方式,维护起来很烦。

回答by Khalos

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

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

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

在 anActivityonCreate函数中调用它会导致它被多次初始化,因为一个 Activity 在应用程序的生命周期中可以被创建多次

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

相反,创建一个Application 类android:name并向您的应用程序清单添加一个属性)。

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>

回答by JC23

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...
  1. 将应用程序类添加到清单 &
  2. 在 app 类中初始化解析。我正在使用 SDK 的 v1.10.1 ...

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

回答by pseudozach

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

回答by charany1

I solved it using a boolean isParseInitializedvariable .Works on orientation change i.e. when activity is recreated in the same Application session . Code snippet :

我使用布尔值isParseInitialized变量解决了它。适用于方向更改,即在同一应用程序会话中重新创建活动时。代码片段:

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

  ..........

}

回答by user196554

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

    }
}