Android 可打包错误
我在使用 Android Parcelable 时遇到问题。
我收到此异常:
01-04 03:21:00.318: ERROR/AndroidRuntime(18973): FATAL EXCEPTION: main
java.lang.RuntimeException: Parcel android.os.Parcel@48302ba8:
Unmarshalling unknown type code 6881383 at offset 244
at android.os.Parcel.readValue(Parcel.java:1851)
at android.os.Parcel.readListInternal(Parcel.java:2030)
at android.os.Parcel.readArrayList(Parcel.java:1474)
at android.os.Parcel.readValue(Parcel.java:1805)
at android.os.Parcel.readMapInternal(Parcel.java:2021)
at android.os.Bundle.unparcel(Bundle.java:208)
at android.os.Bundle.getParcelableArrayList(Bundle.java:1144)
我有以下类:
Cities.java
package project.login;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.OverlayItem;
import android.os.Parcel;
import android.os.Parcelable;
public class Cities extends OverlayItem implements Parcelable{
private String cityName ;
private String cityTime;
private String countryName;
private String day;
private String timeZone;
private float latitude ;
private float longitude ;
public Cities(String cityName, String cityTime, String countryName, String day, String timeZone, float latitude, float longitude) {
super(cityName, countryName, new GeoPoint(latitude, longitude));
this.cityName = cityName;
this.cityTime = cityTime;
this.countryName = countryName;
this.day = day;
this.timeZone = timeZone;
this.latitude = latitude;
this.longitude = longitude;
}
public Cities(String cityName, String cityTime, String countryName, String day, String timeZone, float latitude, float longitude, Parcel parcel) {
super(cityName, countryName, new GeoPoint(latitude, longitude));
this.cityName = parcel.readString();
this.cityTime = parcel.readString();
this.countryName = parcel.readString();
this.day = parcel.readString();
this.timeZone = parcel.readString();
this.latitude = parcel.readFloat() ;
this.longitude = parcel.readFloat() ;
}
@Override
public int describeContents() {
return this.hashCode() ;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(cityName) ;
dest.writeString(cityTime) ;
dest.writeString(countryName) ;
dest.writeString(day) ;
dest.writeString(timeZone) ;
dest.writeFloat(latitude) ;
dest.writeFloat(longitude) ;
}
public static final Parcelable.Creator<Cities> CREATOR = new Parcelable.Creator<Cities>() {
public Cities createFromParcel(Parcel in) {
return new Cities(in.readString(), in.readString(), in.readString(), in.readString(), in.readString(), in.readFloat(), in.readFloat(), in);
}
public Cities[] newArray(int size) {
return new Cities[size];
}
};
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCityTime() {
return cityTime;
}
public void setCityTime(String cityTime) {
this.cityTime = cityTime;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public String getTimeZone() {
return timeZone;
}
public void setTimeZone(String timeZone) {
this.timeZone = timeZone;
}
public float getLatitude() {
return latitude;
}
public void setLatitude(float latitude) {
this.latitude = latitude;
}
public float getLongitude() {
return longitude;
}
public void setLongitude(float longitude) {
this.longitude = longitude;
}
}
我从一个活动激发一个意图:
final List<Cities> cityList = new ArrayList<Cities>();
cityList.add(new Cities(
"TOKYO", "7:15 AM", "Japan", "Today", "UTC + 9:30", 10.2f , 11.2f)) ;
cityList.add(new Cities(
"NEW DELHI", "4:00 PM", "India", "Today", "UTC + 5:30", 111.0f, 123.0f)) ;
cityList.add(new Cities(
"NEW YORK", "4:00 AM", "Usa", "Today", "UTC - 5:30", 23.4f, 77.5f)) ;
Intent intent = new Intent(mContext, LoginSubActivity.class) ;
Bundle bundle = new Bundle() ;
ArrayList<Cities> listOfCities = new ArrayList<Cities>() ;
listOfCities.addAll(cityList) ;
bundle.putParcelableArrayList("cities", listOfCities) ;
intent.putExtras(bundle) ;
startActivity(intent) ;
并在另一个活动中接收它:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle bundle = intent.getExtras() ;
//THE EXCEPTION IS THROWN HERE:
ArrayList<Cities> cityList = bundle.getParcelableArrayList("cities") ;
final ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>() ;
}
是什么导致了此异常?
I'm having a problem with the android parcelable.
I am getting this Exception:
01-04 03:21:00.318: ERROR/AndroidRuntime(18973): FATAL EXCEPTION: main
java.lang.RuntimeException: Parcel android.os.Parcel@48302ba8:
Unmarshalling unknown type code 6881383 at offset 244
at android.os.Parcel.readValue(Parcel.java:1851)
at android.os.Parcel.readListInternal(Parcel.java:2030)
at android.os.Parcel.readArrayList(Parcel.java:1474)
at android.os.Parcel.readValue(Parcel.java:1805)
at android.os.Parcel.readMapInternal(Parcel.java:2021)
at android.os.Bundle.unparcel(Bundle.java:208)
at android.os.Bundle.getParcelableArrayList(Bundle.java:1144)
I have the following classes :
Cities.java
package project.login;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.OverlayItem;
import android.os.Parcel;
import android.os.Parcelable;
public class Cities extends OverlayItem implements Parcelable{
private String cityName ;
private String cityTime;
private String countryName;
private String day;
private String timeZone;
private float latitude ;
private float longitude ;
public Cities(String cityName, String cityTime, String countryName, String day, String timeZone, float latitude, float longitude) {
super(cityName, countryName, new GeoPoint(latitude, longitude));
this.cityName = cityName;
this.cityTime = cityTime;
this.countryName = countryName;
this.day = day;
this.timeZone = timeZone;
this.latitude = latitude;
this.longitude = longitude;
}
public Cities(String cityName, String cityTime, String countryName, String day, String timeZone, float latitude, float longitude, Parcel parcel) {
super(cityName, countryName, new GeoPoint(latitude, longitude));
this.cityName = parcel.readString();
this.cityTime = parcel.readString();
this.countryName = parcel.readString();
this.day = parcel.readString();
this.timeZone = parcel.readString();
this.latitude = parcel.readFloat() ;
this.longitude = parcel.readFloat() ;
}
@Override
public int describeContents() {
return this.hashCode() ;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(cityName) ;
dest.writeString(cityTime) ;
dest.writeString(countryName) ;
dest.writeString(day) ;
dest.writeString(timeZone) ;
dest.writeFloat(latitude) ;
dest.writeFloat(longitude) ;
}
public static final Parcelable.Creator<Cities> CREATOR = new Parcelable.Creator<Cities>() {
public Cities createFromParcel(Parcel in) {
return new Cities(in.readString(), in.readString(), in.readString(), in.readString(), in.readString(), in.readFloat(), in.readFloat(), in);
}
public Cities[] newArray(int size) {
return new Cities[size];
}
};
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCityTime() {
return cityTime;
}
public void setCityTime(String cityTime) {
this.cityTime = cityTime;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
public String getTimeZone() {
return timeZone;
}
public void setTimeZone(String timeZone) {
this.timeZone = timeZone;
}
public float getLatitude() {
return latitude;
}
public void setLatitude(float latitude) {
this.latitude = latitude;
}
public float getLongitude() {
return longitude;
}
public void setLongitude(float longitude) {
this.longitude = longitude;
}
}
I fire an intent from an activity :
final List<Cities> cityList = new ArrayList<Cities>();
cityList.add(new Cities(
"TOKYO", "7:15 AM", "Japan", "Today", "UTC + 9:30", 10.2f , 11.2f)) ;
cityList.add(new Cities(
"NEW DELHI", "4:00 PM", "India", "Today", "UTC + 5:30", 111.0f, 123.0f)) ;
cityList.add(new Cities(
"NEW YORK", "4:00 AM", "Usa", "Today", "UTC - 5:30", 23.4f, 77.5f)) ;
Intent intent = new Intent(mContext, LoginSubActivity.class) ;
Bundle bundle = new Bundle() ;
ArrayList<Cities> listOfCities = new ArrayList<Cities>() ;
listOfCities.addAll(cityList) ;
bundle.putParcelableArrayList("cities", listOfCities) ;
intent.putExtras(bundle) ;
startActivity(intent) ;
And recieve it in another activity :
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle bundle = intent.getExtras() ;
//THE EXCEPTION IS THROWN HERE:
ArrayList<Cities> cityList = bundle.getParcelableArrayList("cities") ;
final ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>() ;
}
What causes this Exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码最终调用 readString 和 readFloat 的次数是所需次数的两倍。一旦您在 createFromParcel 方法中读取数据,然后在构造函数中再次读取数据。您可以消除两者之一。
Your code ends up calling readString and readFloat twice as many times as needed. Once you read the data in the createFromParcel method and then again in the constructor. You can eliminate one of the two.