0

Possible Duplicate:
Android RuntimeException: Unable to instantiate the service

I am trying to run this GCMIntentService code but I keep getting Unable to instantiate service. Here is my GCMIntentService code:

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);
  }

}

And here is my manifest

 <?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>

LogCat

 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
Community
  • 1
  • 1
BigT
  • 1,413
  • 5
  • 24
  • 52

3 Answers3

5

Unable to instantiate service is called by some Exception being thrown in the constructor. There is usually a stack trace that accompanies this (which you should have included), but you should NOT be using a constructor with a String parameter.

You must define this sender ID yourself:

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

public GCMIntentService(){
    super(senderID);
    Logd.i(LOG_TAG, "GCM passed");
}
Cat
  • 66,919
  • 24
  • 133
  • 141
  • That still throws this error `internal error retry receiver class not set` – BigT Aug 16 '12 at 17:50
  • LogCat has an message saying `onError: Account_Missing` – BigT Aug 16 '12 at 17:51
  • Are you running this on the emulator? `ACCOUNT_MISSING` means there is no Google Account running on the phone. You can set one up in `Settings > Accounts & Sync`. – Cat Aug 16 '12 at 17:54
  • I have my email account there but still Account is missing – BigT Aug 16 '12 at 18:51
  • Is it set up as a Google account? Because [that's the only cause](http://developer.android.com/guide/google/gcm/gcm.html#handling_reg) of such an error. If you still can't figure it out, you'll need to test it on a real device which has been configured with an account. – Cat Aug 16 '12 at 18:56
  • No its not on a google account. If they dont set up a google account would this not work? – BigT Aug 16 '12 at 19:16
  • That is correct. The user of the device must have a Google account for their device to receive GCM data. – Cat Aug 16 '12 at 19:21
  • @BigT http://gngrwzrd.com/blog/2012/10/30/Android%20GCM%20Error.html – gngrwzrd Oct 01 '12 at 20:10
4

Your service must implement a zero-parameter public constructor. I recommend that you simply not have a constructor -- the typical approach for most services. Here, for example, is a 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);
  }
}

(source code from this sample project)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
2

Actually this is an issue with the gcm.jar file distributed through the SDK Manager. Google fixed this error but it has yet to make it's way to end users.

Details here: http://gngrwzrd.com/blog/2012/10/30/Android%20GCM%20Error.html

gngrwzrd
  • 5,902
  • 4
  • 43
  • 56