BroadcastReceiver ClassNotFound异常

时间:2012-06-29 08:35:45

标签: android serialization android-intent broadcastreceiver alarmmanager

我有一个应用程序,它使用AlarmManager来安排用户通知的警报。我将Alarm对象的实例传递给AlarmManager使用的PendingIntent:

public void scheduleAlarms(List<Alarm> alarms)
{
    for (Alarm alarm : alarms)
    {           
        Log.d(Constants.EVERY_OTHER_ALARM_APP_LOG_TAG, 
                "Setting alarm for: " + new Date(alarm.getTime()).toString() + 
                " with an interval of " + 
                IntervalEnumStringProvider.getIntervalStringInGivenFormat(
                        alarm.getInterval(), 
                        true));
        PendingIntent alarmScheduledPending = createPendingIntent(alarm);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
                alarm.getTime(), 
                (alarm.getInterval() * Constants.ONE_SECOND_IN_MILLIS), 
                alarmScheduledPending);
    }
}

private PendingIntent createPendingIntent(Alarm alarm)
{
    alarmBroadcastReceiverIntent = new Intent(context, AlarmBroadcastReceiver.class);
    alarmBroadcastReceiverIntent.putExtra(Constants.SCHEDULED_ALARM_TAG, alarm);
    return PendingIntent.getBroadcast(context, 
                alarm.getIdForPendingIntent(), 
                alarmBroadcastReceiverIntent, 
                PendingIntent.FLAG_UPDATE_CURRENT);
}

当闹钟时间发生时,我的AlarmBroadcastReceiver班级正确接收广播,但LogCat警告未找到Alarm类:

06-29 08:30:00.084: W/Intent(66): Failure filling in extras
06-29 08:30:00.084: W/Intent(66): java.lang.RuntimeException: Parcelable encounteredClassNotFoundException reading a Serializable object (name = com.fastplanet.everyotheralarmapp.alarm.Alarm)
06-29 08:30:00.084: W/Intent(66):   at android.os.Parcel.readSerializable(Parcel.java:1890)
06-29 08:30:00.084: W/Intent(66):   at android.os.Parcel.readValue(Parcel.java:1761)
06-29 08:30:00.084: W/Intent(66):   at android.os.Parcel.readMapInternal(Parcel.java:1947)
06-29 08:30:00.084: W/Intent(66):   at android.os.Bundle.unparcel(Bundle.java:169)
06-29 08:30:00.084: W/Intent(66):   at android.os.Bundle.putAll(Bundle.java:242)
06-29 08:30:00.084: W/Intent(66):   at android.content.Intent.fillIn(Intent.java:4530)
06-29 08:30:00.084: W/Intent(66):   at com.android.server.am.PendingIntentRecord.send(PendingIntentRecord.java:185)
06-29 08:30:00.084: W/Intent(66):   at android.app.PendingIntent.send(PendingIntent.java:400)
06-29 08:30:00.084: W/Intent(66):   at com.android.server.AlarmManagerService$AlarmThread.run(AlarmManagerService.java:636)
06-29 08:30:00.084: W/Intent(66): Caused by: java.lang.ClassNotFoundException: com.fastplanet.everyotheralarmapp.alarm.Alarm
06-29 08:30:00.084: W/Intent(66):   at java.lang.Class.classForName(Native Method)
06-29 08:30:00.084: W/Intent(66):   at java.lang.Class.forName(Class.java:237)
06-29 08:30:00.084: W/Intent(66):   at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:2595)
06-29 08:30:00.084: W/Intent(66):   at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1848)
06-29 08:30:00.084: W/Intent(66):   at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:852)
06-29 08:30:00.084: W/Intent(66):   at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2006)
06-29 08:30:00.084: W/Intent(66):   at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:956)
06-29 08:30:00.084: W/Intent(66):   at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2289)
06-29 08:30:00.084: W/Intent(66):   at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2243)
06-29 08:30:00.084: W/Intent(66):   at android.os.Parcel.readSerializable(Parcel.java:1884)
06-29 08:30:00.084: W/Intent(66):   ... 8 more
06-29 08:30:00.084: W/Intent(66): Caused by: java.lang.NoClassDefFoundError: com.fastplanet.everyotheralarmapp.alarm.Alarm
06-29 08:30:00.084: W/Intent(66):   ... 18 more
06-29 08:30:00.084: W/Intent(66): Caused by: java.lang.ClassNotFoundException: com.fastplanet.everyotheralarmapp.alarm.Alarm in loader dalvik.system.PathClassLoader@4001ad90
06-29 08:30:00.084: W/Intent(66):   at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
06-29 08:30:00.084: W/Intent(66):   at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
06-29 08:30:00.084: W/Intent(66):   at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
06-29 08:30:00.084: W/Intent(66):   ... 18 more

然而,BroadcastReceiver成功地从接收到的意图中解压缩Alarm对象,并且应用程序按预期工作。

发生了什么事?

更新 - 添加了清单:

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

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

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:name=".data.TheEveryOtherAlarmAppApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name="com.fastplanet.everyotheralarmapp.alarm.AlarmBroadcastReceiver" >
        </receiver>
        <receiver android:name=".alarm.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>

        <activity android:name=".TheEveryOtherAlarmAppActivity" 
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".NewAlarmActivity"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Dialog" >
        </activity>
        <activity
            android:name=".About"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Dialog" >
        </activity>
        <activity
            android:name=".AlarmAlertDialogActivity"
            android:excludeFromRecents="true"
            android:noHistory="true"
            android:taskAffinity=""
            android:theme="@android:style/Theme.NoDisplay" >
        </activity>
        <activity
            android:name=".AlarmDetailsActivity" 
            android:screenOrientation="portrait" >
        </activity>
    </application>
</manifest>

更新2 - 添加警报类

public class Alarm implements Serializable
{

    private static final long serialVersionUID = -5410846652701834321L;
    private int id = 0;
    private String name = "";
    private long interval = 0;
    private long time;
    private String description;

    public Alarm(String name, long time, long interval, String description, int id)
    {
        this.name = name;
        this.time = time;
        this.interval = interval;
        this.description = description;
        this.id = id;
    }

    public void setInterval(long newAlarmInterval)
    {
        this.interval = newAlarmInterval;
    }

    public long getInterval()
    {
        return interval;
    }

    public void setTime(long time)
    {
        this.time = time;
    }

    public long getTime()
    {
        return time;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void setDescription(String newAlarmDescription)
    {
        this.description = newAlarmDescription;
    }

    public String getDescription()
    {
        return description;
    }

    public int getIdForPendingIntent()
    {
        return id;
    }

    @Override
    public String toString()
    {
        return "name: " + name + ", time: " 
            + new Date(time).toLocaleString()
            + ", interval: "
            + IntervalEnumStringProvider.getIntervalStringInGivenFormat(interval, true)
            + ", description: " + description
            + ", id: " + id;
    }

    @Override
    public Alarm clone() throws CloneNotSupportedException
    {
        return new Alarm(new String(name), time, interval, new String(description), id);
    }
}

5 个答案:

答案 0 :(得分:8)

我在努力解决这个问题。解决方案是(归功于mazur here):

这是android的缺陷。我的目标API是19,最小API是11.缺陷在这里输入:&#34; ClassNotFoundException使用自定义Parcelable&#34; https://code.google.com/p/android/issues/detail?id=6822

注册闹钟时:

Bundle bundle = new Bundle();
bundle.putParcelable("com.foo.alarm", alert);
mNotificationReceiverIntent.putExtra("com.foo.alarm",bundle);
mNotificationReceiverPendingIntent = PendingIntent.getBroadcast(this,
        0, mNotificationReceiverIntent, 0);

收到闹钟时,

    Bundle oldBundle = intent.getBundleExtra("com.foo.alarm");      
    Alert alert = oldBundle.getParcelable("com.foo.alarm");

答案 1 :(得分:0)

as

ClassNotFoundException: com.fastplanet.everyotheralarmapp.alarm.Alarm in loader

<receiver android:name=".alarm.BootReceiver" >

答案 2 :(得分:0)

如果要将警报对象作为Intent中的EXTRA传递,则Alarm类需要实现Parcelable,而不是Serializable。

修改

似乎这不是真的。看起来你可以使用Serializable。然而,OP无法使用此功能,并且SE上有其他帖子,人们在使用此功能时遇到了问题。

答案 3 :(得分:0)

使用ADT17,我遇到了同样的问题,但是BroadcastReceiver无法启动。 我尝试了在SO上讨论的内容:尝试改变Proguard配置,但结果是Proguard is disabled by default。试图实施Parcelable,但问题仍然存在。 唯一有效的方法是将BroadCastReceiver置于要接收的对象的Class所在的同一个包中。请注意,异常仍然作为警告抛出,就像OP描述的那样。

答案 4 :(得分:0)

我遇到了同样的问题,Android 2.3。

在我做出以下更改后,将可序列化的额外内容传递给BroadcastReceiver现在对我有用:

  • 使用PendingIntent的Action字符串创建一个Intent。
  • 在AndroidManifest中静态注册我的BroadcastReceiver。