2012-08-16 88 views
0

Possible Duplicate:
Android RuntimeException: Unable to instantiate the service無法實例化服務.GCMIntentService

我試圖運行此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> 

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 
+0

你可以發佈堆棧跟蹤嗎? – 2012-08-16 17:39:18

回答

5

Unable to instantiate service是由一些Exception在構造函數中被拋出調用。通常會附帶一個堆棧跟蹤(您應該包括這個堆棧跟蹤),但是您不應該使用帶String參數的構造函數。

你必須自己定義該發件人ID:

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

public GCMIntentService(){ 
    super(senderID); 
    Logd.i(LOG_TAG, "GCM passed"); 
} 
+0

仍然會拋出這個錯誤'內部錯誤重試接收器類未設置' – BigT 2012-08-16 17:50:18

+0

LogCat有一條消息說'onError:Account_Missing' – BigT 2012-08-16 17:51:46

+0

您是否正在模擬器上運行此? 'ACCOUNT_MISSING'表示電話上沒有Google帳號。您可以在「設置>帳戶和同步」中設置一個。 – Eric 2012-08-16 17:54:30

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

(從this sample project源代碼)

相關問題