Google Cloud Messaging(GCM) - 无法实例化接收器 - java.lang.ClassNotFoundException [英] Google Cloud Messaging (GCM) - unable to instantiate receiver - java.lang.ClassNotFoundException

查看:204
本文介绍了Google Cloud Messaging(GCM) - 无法实例化接收器 - java.lang.ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  08一旦应用程序启动或从服务器发送推送通知时,就会发生以下跟踪。 -31 02:00:14.733:E / AndroidRuntime(30973):java.lang.RuntimeException:无法实例化接收器com.example.smsmining.GcmBroadcastReceiver:java.lang.ClassNotFoundException:未找到类com.example.smsmining .GcmBroadcastReceiver的路径:DexPathList [[zip文件/data/app/com.example.smsmining-1.apk\"],nativeLibraryDirectories=[/data/app-lib/com.example.smsmining-1,/ vendor / lib / system / lib]] 
08-31 02:00:14.733:E / AndroidRuntime(30973):at android.app.ActivityThread.handleReceiver(ActivityThread.java:2405)
08-31 02:00:14.733:E / AndroidRuntime(30973):at android.app.ActivityThread.access $ 1500(ActivityThread.java:141)
08-31 02:00:14.733:E / AndroidRuntime(30973):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1332)
08-31 02:00:14.733:E / AndroidRuntime(30973):at android.os.Handler.d ispatchMessage(Handler.java:99)
08-31 02:00:14.733:E / AndroidRuntime(30973):at android.os.Looper.loop(Looper.java:137)
08-31 02:00:14.733:E / AndroidRuntime(30973):at android.app.ActivityThread.main(ActivityThread.java:5103)
08-31 02:00:14.733:E / AndroidRuntime(30973):at java .lang.reflect.Method.invokeNative(Native Method)
08-31 02:00:14.733:E / AndroidRuntime(30973):at java.lang.reflect.Method.invoke(Method.java:525)
08-31 02:00:14.733:E / AndroidRuntime(30973):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
08-31 02:00 :14.733:E / AndroidRuntime(30973):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-31 02:00:14.733:E / AndroidRuntime(30973):at dalvik.system.NativeStart.main(本地方法)
08-31 02:00:14.733:E / AndroidRuntime(30973):引起:java.lang.ClassNotFoundException:未找到类com.example。 smsmining.GcmBroadcastReceiver的路径:DexPathList [[zip zip e/data/app/com.example.smsmining-1.apk\"],nativeLibraryDirectories=[/data/app-lib/com.example.smsmining-1,/ vendor / lib,/ system / lib]]
08-31 02:00:14.733:E / AndroidRuntime(30973):at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
08-31 02:00:14.733:E / AndroidRuntime在java.lang.ClassLoader.loadClass(ClassLoader.java:501)
08-31 02:00:14.733:E / AndroidRuntime(30973):在java.lang.ClassLoader.loadClass(ClassLoader.java :461)
08-31 02:00:14.733:E / AndroidRuntime(30973):at android.app.ActivityThread.handleReceiver(ActivityThread.java:2400)
08-31 02:00:14.733 :E / AndroidRuntime(30973):... 10 more

我的清单文件:

 <?xml version =1.0encoding =utf-8?> 
< manifest xmlns:android =http://schemas.android.com/apk/res/android
package =com.example.smsmining
android:versionCode = 1
android:versionName =1.0>

< uses-sdk
android:minSdkVersion =9
android:targetSdkVersion =16/>
< uses-permission android:name =android.permission.READ_SMS/>
< uses-permission android:name =android.permission.RECEIVE_SMS/>
<使用权限android:name =android.permission.READ_CONTACTS/>
< uses-permission android:name =android.permission.INTERNET/>

<使用权限android:name =android.permission.GET_ACCOUNTS/>
<使用权限android:name =android.permission.WAKE_LOCK/>


< permission android:name =com.example.smsmining.permission.C2D_MESSAGE
android:protectionLevel =signature/>
<使用权限android:name =com.example.smsmining.permission.C2D_MESSAGE/>

<! - 这个应用程序有权注册和接收数据信息。 - >
< uses-permission
android:name =com.google.android.c2dm.permission.RECEIVE/>

$ b< application
android:allowBackup =true
android:icon =@ drawable / ic_launcher
android:label =@字符串/应用程序名称
android:theme =@ style / AppTheme>
< activity
android:name =。MainActivity
android:label =@ string / app_name>
< intent-filter>

< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>

< receiver android:name =com.example.smsmining.SMSBroadcastReceiver>
< intent-filter>
< / intent-filter>
< / receiver>



< receiver
android:name =。GcmBroadcastReceiver
android:permission =com.google.android.c2dm.permission。发送>
< intent-filter>
< category android:name =com.example.smsmining/>
< / intent-filter>
< / receiver>
< service android:name =。GCMIntentService/>

< / application>

< / manifest>

GcmBroadcastReceiver.java

  package com.example.smsmining; 

导入android.app.Activity;
导入android.content.ComponentName;
导入android.content.Context;
导入android.content.Intent;
导入android.support.v4.content.WakefulBroadcastReceiver;

$ b $ ** ** b $ b *这个WakefulBroadcastReceiver代码负责为您的应用程序创建和管理
*部分唤醒锁。它将处理GCM
*消息的工作传递给IntentService,同时确保设备不会在
*转回睡眠状态。当准备好
*释放唤醒锁时,{@code IntentService}调用
* {@code GcmBroadcastReceiver.completeWakefulIntent()}。
* /

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
$ b $ @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);


$ / code>

GcmIntentService.java

  public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder构建器;

public GcmIntentService(){
super(GcmIntentService);
}
public static final String TAG =GCM Demo;

@Override
protected void onHandleIntent(Intent intent){
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// getMessageType()意图参数必须是您在BroadcastReceiver中收到
//的意图。
字符串messageType = gcm.getMessageType(intent);

if(!extras.isEmpty()){//具有不合格的效果Bundle
/ *
*根据消息类型过滤消息。由于GCM很可能在未来使用新的消息类型进行
*扩展,因此忽略任何您感兴趣的消息类型或您不认识的消息类型。
* /
if(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)){
sendNotification(Send error:+ extras.toString());
} else if(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)){
sendNotification(服务器上的已删除邮件:+ extras.toString());
//如果是常规的GCM消息,请做一些工作。
} else if(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)){
//此循环表示服务正在进行一些工作。 (标记i = 0; i <5; i ++){
Log.i(标记,正在工作...+(i + 1)
+/ 5 @ + SystemClock.elapsedRealtime());
尝试{
Thread.sleep(5);
} catch(InterruptedException e){
}
}
Log.i(TAG,Completed work @+ SystemClock.elapsedRealtime());
//发送收到的消息通知。
sendNotification(Received:+ extras.toString());
Log.i(TAG,Received:+ extras.toString());
}
}
//释放由WakefulBroadcastReceiver提供的唤醒锁。
GcmBroadcastReceiver.completeWakefulIntent(intent);
}

//将消息放入通知中并发布。
//这只是您可能选择使用
// GCM消息做的一个简单示例。
private void sendNotification(String msg){
mNotificationManager =(NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);

PendingIntent contentIntent = PendingIntent.getActivity(this,0,
new Intent(this,MainActivity.class),0);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(GCM Notification)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);

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


解决方案

I有同样的错误。这帮助了我。
尝试检查v.4库。
属性 - > Java构建路径 - >订单和导出


编辑:
选择项目并转到:属性 - > Java构建路径 - >订单和导出,确保在列表中检查了android.support.v4库,如果没有,标记它。 (我假设你已经将库导入项目),干净的项目,它应该工作


As soon as the app starts or when the push notification is sent from the server, the following traceback happens.

08-31 02:00:14.733: E/AndroidRuntime(30973): java.lang.RuntimeException: Unable to instantiate receiver com.example.smsmining.GcmBroadcastReceiver: java.lang.ClassNotFoundException: Didn't find class "com.example.smsmining.GcmBroadcastReceiver" on path: DexPathList[[zip file "/data/app/com.example.smsmining-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.smsmining-1, /vendor/lib, /system/lib]]
08-31 02:00:14.733: E/AndroidRuntime(30973):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2405)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at android.app.ActivityThread.access$1500(ActivityThread.java:141)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at android.os.Looper.loop(Looper.java:137)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at android.app.ActivityThread.main(ActivityThread.java:5103)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at java.lang.reflect.Method.invokeNative(Native Method)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at java.lang.reflect.Method.invoke(Method.java:525)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at dalvik.system.NativeStart.main(Native Method)
08-31 02:00:14.733: E/AndroidRuntime(30973): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.smsmining.GcmBroadcastReceiver" on path: DexPathList[[zip file "/data/app/com.example.smsmining-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.smsmining-1, /vendor/lib, /system/lib]]
08-31 02:00:14.733: E/AndroidRuntime(30973):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2400)
08-31 02:00:14.733: E/AndroidRuntime(30973):    ... 10 more

My Manifest file:

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

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />


     <permission android:name="com.example.smsmining.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.smsmining.permission.C2D_MESSAGE" />

    <!-- This app has permission to register and receive data message. -->
    <uses-permission
        android:name="com.google.android.c2dm.permission.RECEIVE" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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="com.example.smsmining.SMSBroadcastReceiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>



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

    </application>

</manifest>

GcmBroadcastReceiver.java

package com.example.smsmining;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;


/**
 * This {@code WakefulBroadcastReceiver} takes care of creating and managing a
 * partial wake lock for your app. It passes off the work of processing the GCM
 * message to an {@code IntentService}, while ensuring that the device does not
 * go back to sleep in the transition. The {@code IntentService} calls
 * {@code GcmBroadcastReceiver.completeWakefulIntent()} when it is ready to
 * release the wake lock.
 */

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

GcmIntentService.java

public class GcmIntentService extends IntentService {
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    public GcmIntentService() {
        super("GcmIntentService");
    }
    public static final String TAG = "GCM Demo";

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that GCM will be
             * extended in the future with new message types, just ignore any message types you're
             * not interested in, or that you don't recognize.
             */
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " + extras.toString());
            // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                for (int i = 0; i < 5; i++) {
                    Log.i(TAG, "Working... " + (i + 1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                sendNotification("Received: " + extras.toString());
                Log.i(TAG, "Received: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("GCM Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);

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

解决方案

I've got the same errors. This helped me. Try to check the v.4 library. Properties->Java Build Path->Order and Export

Edited: Select project and go to: Properties->Java Build Path->Order and Export, make sure android.support.v4 library is checked in the list, if not, mark it. (i assume you already imported the library to project), clean project and it should work

这篇关于Google Cloud Messaging(GCM) - 无法实例化接收器 - java.lang.ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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