Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android-classnotfoundexception-神秘_Java_Android_Notifications_Google Cloud Messaging_Classnotfoundexception - Fatal编程技术网

Java android-classnotfoundexception-神秘

Java android-classnotfoundexception-神秘,java,android,notifications,google-cloud-messaging,classnotfoundexception,Java,Android,Notifications,Google Cloud Messaging,Classnotfoundexception,我试图了解GCM是如何工作的。为此,我复制/粘贴了“实现GCM客户端”一节中提出的代码 从服务器发送消息是可行的,但当我的客户端应用程序收到消息时,它会崩溃,告诉我存在以下问题: E/AndroidRuntime(5722): java.lang.RuntimeException: Unable to instantiate receiver com.test.testnotification3.GcmBroadcastReceiver: java.lang.ClassNotFoundExcep

我试图了解GCM是如何工作的。为此,我复制/粘贴了“实现GCM客户端”一节中提出的代码

从服务器发送消息是可行的,但当我的客户端应用程序收到消息时,它会崩溃,告诉我存在以下问题:

E/AndroidRuntime(5722): java.lang.RuntimeException: Unable to instantiate receiver
com.test.testnotification3.GcmBroadcastReceiver: java.lang.ClassNotFoundException: Didn't
find class "com.test.testnotification3.GcmBroadcastReceiver" on path:
/data/app/com.test.testnotification3-2.apk
我在互联网上搜索了类似的问题,很多人似乎因为清单中的问题(清单中的包名错误)而出现了这个错误。但是我检查了我的舱单,看起来还可以:

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

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.test.testnotification3.permission.C2D_MESSAGE" />

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

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

    <receiver
        android:name="com.test.testnotification3.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />

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

    <service android:name="com.test.testnotification3.GcmIntentService" />

    <activity
        android:name="com.test.testnotification3.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>
</application>

当然,由于包名更改,我将接收器的“com.test.testnotification3”替换为“core”。

您必须将接收器放在另一个包中。如果它在主包上,系统将无法实例化它。

我们可以查看com.test.testnotification3.GcmBroadcastReceiver的源代码吗?我在您的清单中没有看到
com.test.testnotification3.permission.C2D\u消息
permission的定义。你是忘了把它包括在你的问题里,还是真的从清单上找不到?你是对的,那就是坠机的原因。但是,即使它没有崩溃,我也没有得到我应该得到的通知。但这是另一个问题,如果我找不到解决办法,我会回来的!谢谢!
package core;

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

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

}