0

日志猫错误

> FATAL EXCEPTION: main
> java.lang.RuntimeException: Unable to start service mobinet.imagine.GCMServiceHandler@405656c0 with Intent { > act=com.google.android.c2dm.intent.RECEIVE cat=[mobinet.imagine] cmp=mobinet.imagine/.GCMServiceHandler (has extras) }: > java.lang.NullPointerException at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2056) at > android.app.ActivityThread.access$2800(ActivityThread.java:117)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
> at android.os.Handler.dispatchMessage(Handler.java:99)
> at android.os.Looper.loop(Looper.java:130)
> at android.app.ActivityThread.main(ActivityThread.java:3687)
> at java.lang.reflect.Method.invokeNative(Native Method)
> at java.lang.reflect.Method.invoke(Method.java:507)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
> at dalvik.system.NativeStart.main(Native Method)
> at android.app.IntentService.onStart(IntentService.java:110)
> at android.app.IntentService.onStartCommand(IntentService.java:118)
> at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2043)

我在 gcm 广播接收器的 OnReceive 上收到错误

应用清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mobinet.imagine"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />
<uses-permission android:name="com.google.android.c2dm.intent.REGISTRATION" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<permission
    android:name="mobinet.imagine.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="mobinet.imagine.permission.C2D_MESSAGE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="SampleActivity"
        android:label="@string/title_activity_main"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="SearchResult"
        android:label="@string/title_activity_main"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="ShowProduct"
        android:label="@string/title_activity_main"
        android:screenOrientation="portrait" >
    </activity>

    <receiver
        android:name="mobinet.imagine.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />            <category android:name="mobinet.imagine" />
        </intent-filter>
    </receiver>
    <service android:name="mobinet.imagine.GCMServiceHandler" android:enabled="true" >
    </service>
</application>
</manifest>

WakefulBroadcastReceiver的代码:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    ComponentName comp = new ComponentName("mobinet.imagine",
            GCMServiceHandler.class.getName());
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}
}

意图服务代码如下:

public class GCMServiceHandler extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GCMServiceHandler() {
    super("GCMServiceHandler");
    ApplicationDebug.LogInfo("Inside Constructor of service");
}
@Override
protected void onHandleIntent(Intent intent) {
    ApplicationDebug.LogInfo("Inside onHandleIntent");
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    // The getMessageType() intent parameter must be the intent you received
    // in your BroadcastReceiver.
    String messageType = gcm.getMessageType(intent);        
    if (!extras.isEmpty()) { // has effect of unparcelling Bundle
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                .equals(messageType)) {
            sendNotification("Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                .equals(messageType)) {
            sendNotification("Deleted messages on server: "
                    + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                .equals(messageType)) {

            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                }
            }
            sendNotification("Received: " + extras.toString());
        }
    }
    else{
    }        
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), 0);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.logo)
            .setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public void onCreate() {
}

提前致谢!

4

2 回答 2

1

它认为问题出在GCMServiceHandler的onCreate()方法中。如果您覆盖 onCreate() 方法,则不能将其留空。你应该写super.onCreate();

所以它应该是这样的:

    @Override
    public void onCreate() {
        super.onCreate();
    }

David Wasser在此处解释了您不能将其留空的原因:

onCreate()在服务对象被实例化时被调用(即:当服务被创建时)。你应该在这个方法中做一些你只需要做一次的事情(即:初始化一些变量等)。


当然,您也可以只删除 onCreate 方法。

很抱歉回答晚了,但我希望它对你有所帮助。

于 2014-07-01T11:38:10.427 回答
0

额外变量在 onHandleIntent 方法中为空 检查您是否发送了正确的额外信息

于 2014-02-28T12:48:02.903 回答