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()は、Service オブジェクトがインスタンス化されるとき (つまり、サービスが作成されるとき) に呼び出されます。このメソッドでは、一度だけ行う必要があることを行う必要があります (つまり、いくつかの変数を初期化するなど)。


もちろん、onCreate メソッドを削除することもできます。

返事が遅くなり申し訳ありませんが、お役に立てば幸いです。

于 2014-07-01T11:38:10.427 に答える
0

余分な変数が onHandleIntent メソッドで null になっています 正しいエクストラを送信しているかどうかを確認してください

于 2014-02-28T12:48:02.903 に答える