0

可能重复:
Android RuntimeException:无法实例化服务

我正在尝试运行此 GCMIntentService 代码,但我不断收到Unable to instantiate service. 这是我的 GCMIntentService 代码:

package com.e;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {
  public static String TAG = "GCMIntentService";
  private static String KEY = "c2dmPref";
  private static String REGISTRATION_KEY = "registrationKey";

  public GCMIntentService(String senderId) {
    super(senderId);
    Log.d("GCMIntentService", senderId);
  }

  @Override
  public void onRegistered(Context context, String regId){
    Log.e("registration :","registration :"+regId);

    if (regId != null) {
        Log.d("c2dm", regId);
        Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
        editor.putString(REGISTRATION_KEY, regId);
        editor.commit();
    }
  }

  @Override
  public void onMessage(Context context, Intent intent){
     String message = intent.getExtras().getString("payload");
     //String key = intent.getExtras().getString("collapse_key");
     Log.e("","message : " +message);    
          Intent startActivity = new Intent(); 
            startActivity.setClass(context, NotificationService.class); 
            startActivity.setAction(NotificationService.class.getName()); 
            startActivity.setFlags( 
                    Intent.FLAG_ACTIVITY_NEW_TASK 
                    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);      
            startActivity.putExtra("Title", "New Message");
            startActivity.putExtra("Message", message);
            context.startActivity(startActivity); 
  }

  @Override
  protected void onError(Context arg0, String arg1) {
    Log.d("onError", arg1);
  }

  @Override
  protected boolean onRecoverableError(Context context, String errorId){
    Log.d("onRecoverableError", errorId);
    return false;
  }

  @Override
  protected void onUnregistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub
     Log.d("onUnregistered", arg1);
  }

}

这是我的清单

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

<application android:icon="@drawable/mainlogo" android:label="@string/app_name">                
    <activity android:name=".SplashScreen" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".HomeScreen"></activity>
    <activity android:name=".TabController" android:windowSoftInputMode="adjustPan"></activity>
    <activity android:name=".Help" android:windowSoftInputMode="adjustPan"></activity>
    <activity android:name=".Services"></activity>
    <activity android:name=".About"></activity>
    <activity android:name=".Inbox"></activity>
    <activity android:name=".More"></activity>
    <activity android:name=".Disclaimer"></activity>
    <activity android:name=".Legal"></activity>
    <activity android:name=".PrivacyPolicy"></activity>
    <activity android:name=".Settings"></activity>
    <activity android:name=".TermsOfUse"></activity>
    <activity android:name=".WebPage"></activity>
    <activity android:name=".DetailView"></activity>
    <service android:name=".NotificationService"></service>
    <service android:name=".GCMIntentService"/>     

    <receiver android:name="com.google.android.gcm.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" />
            <category android:name="com.e" />
        </intent-filter>
    </receiver> 
</application>

<permission android:name="com.e.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.e.permission.C2D_MESSAGE" /> 

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<uses-sdk android:minSdkVersion="10" />
</manifest>

日志猫

 08-16 16:18:29.026: E/AndroidRuntime(651): FATAL EXCEPTION: main
 08-16 16:18:29.026: E/AndroidRuntime(651): java.lang.RuntimeException: Unable to instantiate service com.e.GCMIntentService: java.lang.InstantiationException: com.e.GCMIntentService
 08-16 16:18:29.026: E/AndroidRuntime(651):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:1929)
 08-16 16:18:29.026: E/AndroidRuntime(651):     at android.app.ActivityThread.access$2500(ActivityThread.java:117)
 08-16 16:18:29.026: E/AndroidRuntime(651):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
08-16 16:18:29.026: E/AndroidRuntime(651):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 16:18:29.026: E/AndroidRuntime(651):  at android.os.Looper.loop(Looper.java:130)
08-16 16:18:29.026: E/AndroidRuntime(651):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-16 16:18:29.026: E/AndroidRuntime(651):  at java.lang.reflect.Method.invokeNative(Native Method)
08-16 16:18:29.026: E/AndroidRuntime(651):  at java.lang.reflect.Method.invoke(Method.java:507)
08-16 16:18:29.026: E/AndroidRuntime(651):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-16 16:18:29.026: E/AndroidRuntime(651):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-16 16:18:29.026: E/AndroidRuntime(651):  at dalvik.system.NativeStart.main(Native Method)
08-16 16:18:29.026: E/AndroidRuntime(651): Caused by: java.lang.InstantiationException: com.e.GCMIntentService
08-16 16:18:29.026: E/AndroidRuntime(651):  at java.lang.Class.newInstanceImpl(Native Method)
08-16 16:18:29.026: E/AndroidRuntime(651):  at java.lang.Class.newInstance(Class.java:1409)
08-16 16:18:29.026: E/AndroidRuntime(651):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:1926)
08-16 16:18:29.026: E/AndroidRuntime(651):  ... 10 more
4

3 回答 3

5

Unable to instantiate serviceException被构造函数中抛出的一些调用。通常有一个堆栈跟踪伴随这个(你应该包括在内),但你不应该使用带String参数的构造函数。

您必须自己定义此发件人 ID:

private final static String senderID = "11111111111"; // Your ID here

public GCMIntentService(){
    super(senderID);
    Logd.i(LOG_TAG, "GCM passed");
}
于 2012-08-16T17:43:12.087 回答
4

您的服务必须实现一个零参数的公共构造函数。我建议您根本没有构造函数——大多数服务的典型方法。例如,这里是一个GCMIntentService

public class GCMIntentService extends GCMBaseIntentService {
  @Override
  protected void onRegistered(Context ctxt, String regId) {
    Log.d(getClass().getSimpleName(), "onRegistered: " + regId);
    Toast.makeText(this, regId, Toast.LENGTH_LONG).show();
  }

  @Override
  protected void onUnregistered(Context ctxt, String regId) {
    Log.d(getClass().getSimpleName(), "onUnregistered: " + regId);
  }

  @Override
  protected void onMessage(Context ctxt, Intent message) {
    Bundle extras=message.getExtras();

    for (String key : extras.keySet()) {
      Log.d(getClass().getSimpleName(),
            String.format("onMessage: %s=%s", key,
                          extras.getString(key)));
    }
  }

  @Override
  protected void onError(Context ctxt, String errorMsg) {
    Log.d(getClass().getSimpleName(), "onError: " + errorMsg);
  }

  @Override
  protected boolean onRecoverableError(Context ctxt, String errorMsg) {
    Log.d(getClass().getSimpleName(), "onRecoverableError: " + errorMsg);

    return(true);
  }
}

此示例项目的源代码)

于 2012-08-16T18:01:04.097 回答
2

实际上这是通过 SDK 管理器分发的 gcm.jar 文件的问题。谷歌修复了这个错误,但它尚未成为最终用户的方式。

详情在这里: http: //gngrwzrd.com/blog/2012/10/30/Android%20GCM%20Error.html

于 2012-10-01T20:01:45.137 回答