2013-06-19 96 views
4

林教程這裏如下:http://developer.android.com/google/gcm/gs.html谷歌雲通訊,無法初始化接收機

我似乎有一些問題,註冊我的廣播接收器

我收到以下錯誤每當應用程序運行,我註冊之後該設備,並獲得註冊ID

了java.lang.RuntimeException:無法實例接收機 com.google.android.gcm.GCMBroadcastReceiver: 拋出java.lang.ClassNotFoundException : com.google.android.gcm.GCMBroadcastReceiver

以下是完整的堆棧跟蹤:

一月6日至19日:08:03.994:E/AndroidRuntime(23867):致命異常: main 06-19 01:08:03.994:E/AndroidRuntime(23867): java.lang.RuntimeException:無法實例化接收者 com.google.android.gcm.GCMBroadcastReceiver: java.lang.ClassNotFoundException: com.google .android.gcm.GCMBroadcastReceiver 06-19 01:08:03.994: E/AndroidRuntime(23867):at android.app.ActivityThread.handleReceiver(ActivityThread.java:2111) 06-19 01:08:03.994:E/AndroidRuntime(23867):at android.app.ActivityThread.access $ 1500(ActivityThread.java:127)06-19 01:08:03.994:E/AndroidRuntime(23867):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1208) 06-19 01:08 :03.994:E/AndroidRuntime(23867):at android.os.Handler.dispatchMessage(Handler.java:99)06-19 01:08:03.994:E/AndroidRuntime(23867):at android.os.Looper .loop(Looper.java:137)06-19 01:08:03.994: E/AndroidRuntime(23867):at android.app.ActivityThread.main(ActivityThread.java:4441)06-19 01:08:03.994:E/AndroidRuntime(23867):在 java.lang.reflect.Method.invokeNative(Native Method)06-19 01:08:03.994:E/AndroidRuntime(23867):at java。 lang.reflect.Method.invoke(Method.java:511)06-19 01:08:03.994: E/AndroidRuntime(23867):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java :784) 06-19 01:08:03.994:E/AndroidRuntime(23867):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)06-19 01:08:03.994: E/AndroidRuntime(23867):at dalvik.system.NativeStart.main(Native Method)06-19 01:08:03.994: E/AndroidRuntime(23867):引起:java.lang.ClassNotFoundException: com.google.android.gcm.GCMBroadcastReceiver 06-19 01:08:03.994: E/AndroidRuntime(23867):at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 06-19 01:08: 03.994:E/AndroidRuntime(23867):at java.lang.ClassLoader.loadClass(ClassLoader.java:501)06-19 01:08:03.994:E/AndroidRuntime(23867):at java.lang.ClassLoader。 loadClass(ClassLoader.java:461)06-19 01:08:03.994:E/AndroidRuntime(23867):at android.app.ActivityThread.handleReceiver(ActivityThread。Java的:2106)

以下是完整的清單,其中一些活動隔斷

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

    <uses-sdk android:minSdkVersion="8" 
     android:targetSdkVersion="11" /> 

    <uses-feature android:name="android.hardware.camera" /> 

    <supports-screens 
     android:smallScreens="true" 
     android:normalScreens="true" 
     android:largeScreens="true" 
     android:anyDensity="true" /> 

    <application 
     android:hardwareAccelerated="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <uses-library android:name="com.google.android.maps" /> 

     <!-- Push notification --> 
       <receiver 
      android:name="pushNotification.GCMBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <category android:name="com.gotoohlala" /> 
      </intent-filter> 
     </receiver> 

       <!-- Push notification END --> 

     <activity 
      android:name="launchOohlala.LaunchOohlala" 
      android:label="@string/app_name" 
      android:theme="@android:style/Theme.Light.NoTitleBar" 
      android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="launchOohlala.FakeLogin" 
      android:label="@string/app_name" 
      android:theme="@android:style/Theme.Light.NoTitleBar" 
      android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="launchOohlala.CheckEmail" 
      android:label="@string/app_name" 
      android:windowSoftInputMode="stateHidden|adjustResize" 
      android:theme="@android:style/Theme.Light.NoTitleBar" 
      android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name="discoverMyCampus.RulesFragment" 
      android:label="@string/app_name" 
      android:windowSoftInputMode="stateHidden|adjustResize" 
      android:theme="@android:style/Theme.Light.NoTitleBar" 
      android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 


     <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.gotoohlala" /> 
      </intent-filter> 
     </receiver> 
     <service android:name=".GCMIntentService" /> 

     <activity android:name="com.facebook.LoginActivity" 
        android:theme="@android:style/Theme.Translucent.NoTitleBar" 
        android:label="@string/app_name" /> 
     <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/applicationId" /> 

     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="AIzaSyDP2zfbRiEH0mNCkn2lpxm92DQHsfIJfPU" /> 

    </application> 

     <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 



    <uses-permission android:name="android.permission.CAMERA" /> 
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 
    <uses-permission android:name="android.permission.VIBRATE" /> 
    <!-- Wifi related --> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.SEND_SMS" /> 

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.CALL_PHONE"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.READ_CALENDAR" /> 
    <uses-permission android:name="android.permission.WRITE_CALENDAR" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 


    <uses-permission android:name="com.gotoohlala.gcm.permission.C2D_MESSAGE" /> 


    <permission android:name="com.gotoohlala.permission.C2D_MESSAGE" android:protectionLevel="signature" /> 
    <uses-permission android:name="com.gotoohlala.permission.C2D_MESSAGE" /> 
    <!-- App receives GCM messages. --> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <!-- GCM requires a Google account. --> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <!-- Keeps the processor from sleeping when a message is received. --> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 

    <uses-feature android:name="android.hardware.telephony" android:required="false" /> 
    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true"/> 

    <permission 
     android:name="com.gotoohlala.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature"/> 
    <uses-permission android:name="com.gotoohlala.permission.MAPS_RECEIVE"/> 

</manifest> 

我GCMBroadcastReceiver類

package pushNotification; 

import com.google.android.gms.gcm.GoogleCloudMessaging; 
import com.gotoohlala.OohlalaMain; 

import discoverMyCampus.RulesFragment; 

import android.app.Activity; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v4.app.NotificationCompat; 
import android.util.Log; 

/** 
* Handling of GCM messages. 
*/ 
public class GCMBroadcastReceiver extends BroadcastReceiver { 
    static final String TAG = "GCMDemo"; 
    public static final int NOTIFICATION_ID = 1; 
    private NotificationManager mNotificationManager; 
    NotificationCompat.Builder builder; 
    Context ctx; 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d("SUCCESS SUCCESS", "ONRECEIVE HAS BEEN CALLED, WOOT WOOT!!"); 
     GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); 
     Log.d("SUCCESS SUCCESS", "ONRECEIVE HAS BEEN CALLED, WOOT WOOT!!"); 
     ctx = context; 
     String messageType = gcm.getMessageType(intent); 
     if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { 
      sendNotification("Send error: " + intent.getExtras().toString()); 
     } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { 
      sendNotification("Deleted messages on server: " + 
        intent.getExtras().toString()); 
     } else { 
      sendNotification("Received: " + intent.getExtras().toString()); 
     } 
     setResultCode(Activity.RESULT_OK); 
    } 

    // Put the GCM message into a notification and post it. 
    private void sendNotification(String msg) { 
     mNotificationManager = (NotificationManager) 
       ctx.getSystemService(Context.NOTIFICATION_SERVICE); 

     PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, 
       new Intent(ctx, RulesFragment.class), 0); 

     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(ctx) 
     .setContentTitle("GCM Notification") 
     .setContentText(msg); 

     mBuilder.setContentIntent(contentIntent); 
     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.getNotification()); 
    } 
} 

任何想法,爲什麼我不是能註冊接收器? 謝謝

+0

您是否得到了解決方案? –

回答

5

該應用正在尋找com.google.android.gcm.GCMBroadcastReceiver,但你的班級是在不同的包pushNotification.GCMBroadcastReceiver

而你的應用程序包是不同的 - com.myAppName

雖然您的GCMBroadcastReceiver不必與應用程序包中的包位於同一包中,但意圖過濾器中的類別必須是該應用程序的包。

你應該改變這樣的:

 <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <category android:name="pushNotification" /> 
     </intent-filter> 

要這樣:

 <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <category android:name="com.myAppName" /> 
     </intent-filter> 

編輯:

根據你的表現,你有額外的錯誤:

您指定的廣播接收器兩次。如果你想使用自己的接收器,您應該刪除此部分:

<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.gotoohlala" /> 
     </intent-filter> 
    </receiver> 

另一個問題:

刪除此行:

<uses-permission android:name="com.gotoohlala.gcm.permission.C2D_MESSAGE" /> 

既然你已經有了這些行(這是正確的):

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

感謝您的澄清,但我仍然在做了推薦的更改後仍然出現相同的錯誤。我應該指出的另一件事是,我的手機上有一個測試服務器,它將消息推送到應用程序,並且每當我嘗試推送消息時也會得到相同的錯誤。 – hervens

+0

@hervens你可以在你的問題中包含你的完整清單嗎? – Eran

+0

好吧,我添加了完整的清單,減去一些鏈接的活動 – hervens

-1

嘗試檢查v.4庫。 Propreties-> Java構建路徑 - >訂單和導出