简体   繁体   English

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

[英]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. 我想知道为什么Parse会为此抛出异常。 Why not just info and move on. 为什么不只是info并继续前进。 Its initialized, so big deal if I initialized it again. 它的初始化,如果我再次初始化它是如此重要。

Solution

I have given up on Parse. 我放弃了Parse。 Dont like the Application way, just to irritating to maintain. 不喜欢应用方式,只是为了刺激维护。

Parse.initialize() should only be called once for an entire application . Parse.initialize()只应为整个应用程序调用一次

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 . ActivityonCreate函数中调用它可以使其多次初始化,因为在应用程序的生命周期中可以多次创建一个Activity

Instead, create an Application class (and add an android:name attribute 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: AndroidManifest:

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

NEVERMIND, I've fixed it. NEVERMIND,我修好了。 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. 在app类中初始化解析。 I'm using v1.10.1 of the SDK... 我正在使用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();
}

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 . 我在方向更改时使用布尔isParseInitialized变量.Works解决了它,即在同一个应用程序会话中重新创建活动时。 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();

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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