无法实例化GcmBroadcastReceiver [英] Unable to instantiate GcmBroadcastReceiver

查看:123
本文介绍了无法实例化GcmBroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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


无法实例化接收者
com.example.testpushnotification.GcmBroadcastReceiver:
java.lang.ClassNotFoundException:没有在路径中找到class
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]]

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



我的清单:

 <?xml version =1 .0encoding =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/>
<使用权限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/>

<使用权限android:name =android.permission.ACCESS_NETWORK_STATE/>
< uses-permission android:name =com.google.android.c2dm.permission.RECEIVE/>
<使用权限android:name =android.permission.GET_ACCOUNTS/>
<使用权限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>

< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>
< receiver
android:name =.GcmBroadcastReceiver
android:permission =com.google.android.c2dm.permission.SEND>
< intent-filter>
< category android:name =com.example.testpushnotification/>
< / intent-filter>
< / receiver>
< service android:name =.GcmIntentService/>
< / application>



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

  public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context,Intent intent){
//明确指定GcmIntentService将处理意图。
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
//启动服务,保持设备在启动时保持清醒状态。
startWakefulService(context,(intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}

为什么Dex路径包含与我不同的包?



请问,如何解决它?

FIXED 更改< receiver
android:name =.GcmBroadcastReceiver
..
< / receiver>



code>< receiver
android:name =com.google.android.gcm.GCMBroadcastReceiver
..
< / receiver> (例如:

解决方案

试试这个:

Properties / Java Build Path / Order and Export - 确保在Android Dependencies和支持库前面有一个检查,如果你使用它的话。标记所有的复选框。点击Apply并清理项目。

添加如下:

 < receiver 
android:name =com。 google.android.gcm.GCMBroadcastReceiver
android:permission =com.googl e.android.c2dm.permission.SEND>
< intent-filter>

< category android:name =com.test.android/>
< / intent-filter>
< / receiver>

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



这对我有效。希望这有助于您。


I tried to create test app with GCM service, according to the Google manual https://developer.android.com/google/gcm/client.html#app

But when i receive message - I have this error:

Unable to instantiate receiver com.example.testpushnotification.GcmBroadcastReceiver: java.lang.ClassNotFoundException: Didn't find class "com.example.testpushnotification.GcmBroadcastReceiver" on path: DexPathList[[zip file "/data/app/com.example.testpushnotification-2.apk"], nativeLibraryDirectories=[/data/app-lib/com.example.testpushnotification-2, /vendor/lib, /system/lib]]

I was looking for all same questions, but they have no solutions or solutions didn't help or they are deprecated.

My manifest:

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

And source code for GcmBroadcastReceiver in the com.example.testpushnotification package:

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

And why Dex path contains differ package than my?

Please, how to fix it ?

FIXED Changed <receiver android:name=".GcmBroadcastReceiver" .. </receiver>

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

解决方案

Try this:

Go to Project/Properties/Java Build Path/Order and Export -- Make sure there's a check in front of Android Dependencies and the support library, if you use it.Mark all checkboxes.Click on Apply and clean the project.

Add like this :

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

It should be : com.google.android.gcm.GCMBroadcastReceiver and not .GCMBroadcastReceiver

This worked for me.Hope this helps.

这篇关于无法实例化GcmBroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆