应用程序崩溃,如果应用程序使用推送服务关闭:parse.com [英] App crashes if app is closed using Push service: parse.com

查看:184
本文介绍了应用程序崩溃,如果应用程序使用推送服务关闭:parse.com的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在清单雷替换接收器:

 <接收机器人:名字=com.myparse.app.MyReceiver
    机器人:出口=假>
  &所述;意图滤光器>
    <作用机器人:名字=com.parse.push.intent.RECEIVE/>
    <作用机器人:名字=com.parse.push.intent.DELETE/>
    <作用机器人:名字=com.parse.push.intent.OPEN/>
    &所述; /意图滤光器>
< /接收器>

这是我的MyReceiver:

 公共类MyReceiver扩展ParsePushBroadcastReceiver {    @覆盖
    保护无效onPushReceive(上下文mContext,意图意图){
// super.onPushReceive(为arg0,ARG1);        如果(intent.hasExtra(com.parse.Data)){
            串jsonString = intent.getExtras()的getString(com.parse.Data);
            // -------------
    }   公共无效onPushOpen(上下文的背景下,意图意图){        //要跟踪应用打开
        ParseAnalytics.trackAppOpenedInBackground(意向);        //这里是你发送的数据
        Log.i(,intent.getExtras()的getString(com.parse.Data));        意图I =新意图(背景下,MainActivity.class);
        i.putExtras(intent.getExtras());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(ⅰ);
    }
}

一切工作正常
但是,如果我关闭应用程序,然后发送推送通知 - >应用程序崩溃与以下日志:

  11-09 21:13:11.621:E / com.parse.PushService(23087):解析推送服务无法启动​​,因为Parse.initialize尚未调用。如果从一个活动的onCreate调用Parse.initialize,该呼叫应改为处于Application.onCreate。要确保您的应用程序类被注册在与Android的Andr​​oidManifest.xml:的name属性的<应用>标签。
11-09 21:13:11.626:E / AndroidRuntime(23087):致命异常:主要
11-09 21:13:11.626:E / AndroidRuntime(23087):了java.lang.RuntimeException:无法启动意图{服务com.parse.PushService@4203adb0行动= com.google.android.c2dm.intent.RECEIVE FLG = 0×10 = PKG CMP com.myparse.app = com.myparse.app / com.parse.PushService(有演员)}了java.lang.RuntimeException:ApplicationContext就是空。使用解析库前必须先调用Parse.initialize(背景下,的applicationID,clientKey)。
11-09 21:13:11.626:E / AndroidRuntime(23087):在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2782)
11-09 21:13:11.626:E / AndroidRuntime(23087):在dalvik.system.NativeStart.main(本机方法)
11-09 21:13:11.626:E / AndroidRuntime(23087):了java.lang.RuntimeException:致ApplicationContext就是空。使用解析库前必须先调用Parse.initialize(背景下,的applicationID,clientKey)。
11-09 21:13:11.626:E / AndroidRuntime(23087):在com.parse.Parse.checkContext(Parse.java:606)


解决方案

我经历了同样的问题,并通过添加解决了它
的android:NAME =。MyApplication的以应用程序标记在Android清单,然后创建一个新的类,它看起来像这样:

 公共类MyApplication的扩展应用
{
    @覆盖
    公共无效的onCreate(){
         Parse.initialize(getApplicationContext(),myAppId,myClientKey);
    }
}

previously我把解析初始化呼叫我的主要活动的onCreate()方法,但作为您发布的错误信息显示,Android的生命周期的精妙之处意味着你不能做到这一点。

I replace receiver in manifest to mine:

<receiver android:name="com.myparse.app.MyReceiver"
    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>

This is my MyReceiver:

public class MyReceiver extends ParsePushBroadcastReceiver {

    @Override
    protected void onPushReceive(Context mContext, Intent intent) {
//      super.onPushReceive(arg0, arg1);

        if (intent.hasExtra("com.parse.Data")){
            String jsonString=intent.getExtras().getString("com.parse.Data");
            //-------------
    }

   public void onPushOpen(Context context, Intent intent) {

        //To track "App Opens"
        ParseAnalytics.trackAppOpenedInBackground(intent);

        //Here is data you sent
        Log.i("", intent.getExtras().getString( "com.parse.Data" ));

        Intent i = new Intent(context, MainActivity.class);
        i.putExtras(intent.getExtras());
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

All works fine But if I close the app , and then sending a push notification-> the app crashes with following logs:

11-09 21:13:11.621: E/com.parse.PushService(23087): The Parse push service cannot start because Parse.initialize has not yet been called. If you call Parse.initialize from an Activity's onCreate, that call should instead be in the Application.onCreate. Be sure your Application class is registered in your AndroidManifest.xml with the android:name property of your <application> tag.
11-09 21:13:11.626: E/AndroidRuntime(23087): FATAL EXCEPTION: main
11-09 21:13:11.626: E/AndroidRuntime(23087): java.lang.RuntimeException: Unable to start service com.parse.PushService@4203adb0 with Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10 pkg=com.myparse.app cmp=com.myparse.app/com.parse.PushService (has extras) }: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(context, applicationId, clientKey) before using the Parse library.
11-09 21:13:11.626: E/AndroidRuntime(23087):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2782)
11-09 21:13:11.626: E/AndroidRuntime(23087):    at dalvik.system.NativeStart.main(Native Method)
11-09 21:13:11.626: E/AndroidRuntime(23087): Caused by: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(context, applicationId, clientKey) before using the Parse library.
11-09 21:13:11.626: E/AndroidRuntime(23087):    at com.parse.Parse.checkContext(Parse.java:606)

解决方案

I experienced the same problem, and solved it by adding android:name=".MyApplication" to the application tag in the Android manifest, and then creating a new class that looks like this:

public class MyApplication extends Application 
{
    @Override
    public void onCreate() { 
         Parse.initialize(getApplicationContext(), myAppId, myClientKey);
    }
}

Previously I'd put the Parse initialisation call in the onCreate() method of my main Activity, but as the error message you posted suggests, the subtleties of the Android lifecycle mean you can't do that.

这篇关于应用程序崩溃,如果应用程序使用推送服务关闭:parse.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆