无法实例化GcmBroadcastReceiver

我试图用GCM服务创buildtesting应用程序,根据Google手册https://developer.android.com/google/gcm/client.html#app

但是,当我收到消息 – 我有这个错误:

无法实例化接收器com.example.testpushnotification.GcmBroadcastReceiver:java.lang.ClassNotFoundException:未在path中find类“com.example.testpushnotification.GcmBroadcastReceiver”:DexPathList [[zip文件] /data/app/com.example。 testpushnotification-2.apk“],nativeLibraryDirectories = [/ data / app-lib / com.example.testpushnotification-2,/ vendor / lib,/ system / lib]]

我正在寻找所有相同的问题,但他们没有解决scheme或解决scheme没有帮助,或他们被弃用。

我的清单:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.testpushnotification" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/> <uses-permission android:name="android.permission.GET_ACCOUNTS"/> <uses-permission android:name="android.permission.WAKE_LOCK"/> <permission android:name="com.example.testpushnotification.permission.C2D_MESSAGE" android:protectionLevel="signature"/> <uses-permission android:name="com.example.testpushnotification.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="com.example.testpushnotification.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".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.example.testpushnotification"/> </intent-filter> </receiver> <service android:name=".GcmIntentService"/> </application> 

以及com.example.testpushnotification包中GcmBroadcastReceiver的源代码:

 public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Explicitly specify that GcmIntentService will handle the intent. ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName()); // Start the service, keeping the device awake while it is launching. startWakefulService(context, (intent.setComponent(comp))); setResultCode(Activity.RESULT_OK); } 

为什么Dexpath包含不同于我的包?

请问,如何解决?

修改<receiver android:name=".GcmBroadcastReceiver" .. </receiver>

<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" .. </receiver> (case sensetive)

Solutions Collecting From Web of "无法实例化GcmBroadcastReceiver"

尝试这个:

转到项目/属性/ Java构buildpath/顺序和导出 – 确保在Android Dependencies和支持库前面有一个检查,如果使用它。标记所有checkbox。单击应用并清理项目。

添加像这样:

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

它应该是: com.google.android.gcm.GCMBroadcastReceiver而不是.GCMBroadcastReceiver

这为我工作,希望这有助于。

我遵循了developer.android教程:/google/gcm/client.html

我的问题是我有“ 属性 – > Java构buildpath – >订单和导出 ”勾选了“Android依赖”。

适当的图书馆清单:

  • Android 4.4.2
  • Android私人图书馆
  • Android的支持-V4

希望这可以帮助那里的人!

这段代码是正确的

 <receiver android:name=".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.infostretch.iSOSAndroid" /> </intent-filter> </receiver> 

我面临同样的问题。 但解决它,让我分享我的解决scheme的步骤..

  • 我创build了一个新的包,然后将BroadcastReceiver移动到该包。
  • 添加接收者声明TAG在提供GCM registeredID的activity标签之后添加适当的包名称。

我的主要问题是扩展了WakefulBroadcastReceiver并需要v4库的GcmBroadcastReceiver类。 由于某些原因,我添加了多个库,其中一个包含v4,我只是简单地从该库中删除该v4。 添加v7作为库到我的应用程序,然后清理并运行…然后它的作品。 对不起,我的英文不好,我只是分享我的经验,以帮助。

米修正是使用清单编辑器向导来更改服务和接收器的名称。 结果是:

 <application android:allowBackup="true" android:icon="@drawable/journey_icon" android:logo="@drawable/journey_icon" android:permission="android.permission.INTERNET" android:theme="@style/AppTheme" > <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <receiver android:name=".GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.example.JourneyApp" /> </intent-filter> </receiver> <service android:name=".GCMIntentService" /> 

变成

 <application android:allowBackup="true" android:icon="@drawable/journey_icon" android:logo="@drawable/journey_icon" android:permission="android.permission.INTERNET" android:theme="@style/AppTheme" > <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <receiver android:name="journeyAux.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.example.JourneyApp" /> </intent-filter> </receiver> <service android:name="journeyAux.GCMIntentService" />