无法创建服务com.parse.PushService:java.lang.NullPointerException

时间:2015-08-06 14:28:16

标签: java android xml parse-platform

我正在使用服务like this example进行推送通知。 仅在应用程序关闭时才会显示错误。另一方面,当应用程序处于活动状态时,似乎不会出现错误。

我的代码:

 ParseCrashReporting.enable(this);

            // Enable Local Datastore.
            Parse.enableLocalDatastore(this);

            Parse.initialize(this, "cMQIGJhjD58Dn3uQ27nx5NsO***", "f0VtuHy2G1NsWUPrfzkzJjh7523UDnqX7cQav****");
            ParseInstallation.getCurrentInstallation().saveInBackground();

            ParseUser.enableAutomaticUser();
            ParseACL defaultACL = new ParseACL();
            // Optionally enable public read access.
            // defaultACL.setPublicReadAccess(true);
            ParseACL.setDefaultACL(defaultACL, true);

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.home.smart.home" >


    <permission
        android:name="com.home.smart.home.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.home.smart.home.permission.MAPS_RECEIVE" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- Push -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- end Push -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <uses-permission android:name="android.permission.BLUETOOTH"/>

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-permission android:name="android.permission.CALL_PHONE" />

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <!-- end Push -->
    <permission android:protectionLevel="signature"
        android:name="com.home.smart.home.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.home.smart.home.permission.C2D_MESSAGE" />
    <!-- Push -->

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyB0gzAqr_M4y_Iy3_xq6yvu17zpkG7V_IU" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <!-- Push -->

        <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>
        <receiver android:name="com.parse.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <!--
                  IMPORTANT: Change "com.parse.starter" to match your app's package name.
                -->
                <category android:name="com.home.smart.home" />
            </intent-filter>
        </receiver>

        <meta-data android:name="com.parse.push.notification_icon"
            android:resource="@drawable/abc_btn_rating_star_on_mtrl_alpha"/>

        <!-- end Push -->

    </application>

</manifest>

来自Logcat的错误消息是:

Unable to create service com.parse.PushService: java.lang.NullPointerException

2 个答案:

答案 0 :(得分:0)

就是这样:

 Parse.initialize(this, "cMQIGJhjD58Dn3uQ27nx5NsOtawsbWy1oxHtYnBn", "f0VtuHy2G1NsWUPrfzkzJjh7523UDnqX7cQavRDw");
            ParseInstallation.getCurrentInstallation().saveInBackground();

在不同的活动中(例如:“Service.java”)或您的主要活动之一?如果它位于您的主要活动中,请创建一个名为Service的新类,将上述代码添加到其中,并将其添加到清单的<application>标记中:

<application
android: name="com.packagename.app.Service"
...
>
...
</application>

答案 1 :(得分:0)

正如我从 Manifest 文件中看到的那样,您没有扩展Application的类。你必须添加一个类extends Application 并将其添加为name属性

然后在这个扩展 Application 的新类中添加 Parse 的初始化。