2014-02-20 18 views
0

我做的,我有存储/恢复数据是JavaMail的消息的HashMap的,但我收到以下错误Android应用程序:传递的JavaMail消息的HashMap来Android的活动

02-20 08:11:25.650: E/AndroidRuntime(2264): FATAL EXCEPTION: main 
    02-20 08:11:25.650: E/AndroidRuntime(2264): java.lang.RuntimeException: Parcel: unable to marshal value [email protected] 
02-20 08:11:25.650: E/AndroidRuntime(2264):  at android.os.Parcel.writeValue(Parcel.java:1235) 
02-20 08:11:25.650: E/AndroidRuntime(2264):  at android.os.Parcel.writeArray(Parcel.java:641) 
02-20 08:11:25.650: E/AndroidRuntime(2264):  at android.os.Parcel.writeValue(Parcel.java:1220) 
02-20 08:11:25.650: E/AndroidRuntime(2264):  at android.os.Parcel.writeMapInternal(Parcel.java:591) 
02-20 08:11:25.650: E/AndroidRuntime(2264):  at android.os.Parcel.writeMap(Parcel.java:575) 
02-20 08:11:25.650: E/AndroidRuntime(2264):  at android.os.Parcel.writeValue(Parcel.java:1166) 
02-20 08:11:25.650: E/AndroidRuntime(2264):  at android.os.Parcel.writeMapInternal(Parcel.java:591) 
02-20 08:11:25.650: E/AndroidRuntime(2264):  at android.os.Bundle.writeToParcel(Bundle.java:1627) 
02-20 08:11:25.650: E/AndroidRuntime(2264):  at android.os.Parcel.writeBundle(Parcel.java:605) 
02-20 08:11:25.650: E/AndroidRuntime(2264):  at com.example.androidtablayout.SimpleParcelable.writeToParcel(SimpleParcelable.java:57) 
</code> 

MailActivity.java

In onSaveInstanceState, I am saving my hashmap in a SimpleParcelable class. The code for saving and restoring is :

@Override 
    protected void onSaveInstanceState(Bundle outState) { 
     Log.i("mustang", "Saving activity states. size : " + emailsMap.size()); 
     if(emailsMap.size() > 0) 
     { 
      outState.putParcelable("parcel", new SimpleParcelable(emailsMap)); 
     } 

     super.onSaveInstanceState(outState); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle state) { 
     // TODO Auto-generated method stub 

     Log.i("mustang", "Restoring activity states."); 

     super.onRestoreInstanceState(state); 

     SimpleParcelable stateParcel = state.getParcelable("parcel"); 
     emailsMap = (HashMap<String, Message[]>)stateParcel.getEmailsMap(); 
     this.emailsFetched(emailsMap.get("INBOX"), "INBOX", TaskType.NONE); 
    } 

而序列化代码SimpleParcelable是:

public class SimpleParcelable implements Parcelable{ 
    private final Map<String, Message[]> emailsMap; 
    public SimpleParcelable(HashMap<String, Message[]> messages) 
    { 
     emailsMap = messages; 
    } 

    public static final Parcelable.Creator<SimpleParcelable> CREATOR 
     = new Parcelable.Creator<SimpleParcelable>() { 

      @Override 
      public SimpleParcelable createFromParcel(Parcel source) { 
       Bundle rBundle = source.readBundle(); 
       return new SimpleParcelable((HashMap<String, Message[]>)rBundle.getSerializable("map")); 
      } 

      @Override 
      public SimpleParcelable[] newArray(int size) { 
       // TODO Auto-generated method stub 
       return new SimpleParcelable[size]; 
      } 
     }; 
    @Override 
    public int describeContents() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     Bundle b = new Bundle(); 
     b.putSerializable("map", (HashMap<String, Message[]>)emailsMap); 

     dest.writeBundle(b); 
    } 

    public Map<String, Message[]> getEmailsMap() 
    { 
     return emailsMap; 
    } 
} 

任何人都可以请指出正在做什么错?如果有任何问题,请告诉我如何实现这些编码和解组代码?

提前感谢。

回答

1

在你的onSaveInstanceState 方法你试图做一个包裹出来的com.sun.mail.imap.IMAPMessage一个或多个实例,其中不包括Parcellable接口。你试图包装的所有实例都必须实现这个接口。

+0

从您的评论看来,IMAPMessage似乎没有实现Parcellable接口。我怎么能做到这一点?由于我是这个平台的新手,所以很抱歉这种天真的做法。代码将有很大的帮助。谢谢 –