我有关于parcelable class(android)的问题。
到目前为止,我已经在2 Intent之间传递了一个DoubleArray而没有任何问题:
new double[] latLong = new double[]{loc.getLatitude(),loc.getLongitude()};
...
intent.putExtra(Site.LATTITUDE, latLong);
...
startActivityForResult(intent, this.ACCESS_GOOGLE);
今天,我尝试使用parcelable类为相同的活动添加新数据。 我的可分类:
package common;
import android.os.Parcel;
import android.os.Parcelable;
public class Site implements Parcelable {
private int id;
private String site_name;
private int category ;
private double latitude;
private double longitude;
/**
*
* @param id
* @param site_name
* @param category
*/
public Site(int id, String site_name, int category,double latitude, double lonngitude){
this.id=id;
this.site_name=site_name;
this.category=category;
this.latitude=latitude;
this.longitude=longitude;
}
public Site(Parcel in){
readFromParcel(in);
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.site_name);
dest.writeInt(this.category);
dest.writeDouble(this.latitude);
dest.writeDouble(this.longitude);
}
/**
*
* @param in
*/
private void readFromParcel(Parcel in) {
this.id = in.readInt();
this.site_name = in.readString();
this.category = in.readInt();
this.latitude = in.readInt();
this.longitude = in.readInt();
}
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
public SiteGoogle createFromParcel(Parcel in) {
return new Site(in);
}
public SiteGoogle[] newArray(int size) {
return new Site[size];
}
};
/* GETTER / SETTER */
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSite_name() {
return site_name;
}
public void setSite_name(String site_name) {
this.site_name = site_name;
}
public int getCategory() {
return category;
}
public void setCategory(int category) {
this.category = category;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
我在java ArrayList中放置了一些Site对象(上面的类),并使用以下代码将此列表传递给我的第二个Activity:
ArrayList<Site> sitesForMap = new ArrayList<Site>();
for(int i=0; i<sites.size();i++){
SiteGoogle site = new SiteGoogle(
sites.get(i).getId(),
sites.get(i).getSite_name(),
sites.get(i).getCat(),
sites.get(i).getLatitude(),
sites.get(i).getLongitude());
sitesForGoogle.add(site);
}
//put data in the intent
intent.putExtra(Site.LATTITUDE, latLong); //first data that worked well until now
intent.putParcelableArrayListExtra(Site.JSON_ARRAY, sitesForMap); //my new data
startActivityForResult(intent, this.ACCESS_GOOGLE);
此代码执行完美(sitesForMap和latLong不为null)但在第二个Activity中,当我尝试实例化第一个Double []参数时, 发生错误。
Bundle bundle = getIntent().getExtras();
double[] adresse = (double[]) bundle.getDoubleArray(Site.LATTITUDE); //worked well until today
List<Site> sitesList = bundle.getParcelableArrayList(Site.JSON_ARRAY);
错误:
11-04 03:00:47.248: D/AndroidRuntime(642): Shutting down VM
11-04 03:00:47.248: W/dalvikvm(642): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-04 03:00:47.350: E/AndroidRuntime(642): FATAL EXCEPTION: main
11-04 03:00:47.350: E/AndroidRuntime(642): java.lang.OutOfMemoryError
11-04 03:00:47.350: E/AndroidRuntime(642): at android.os.Parcel.readArray(Parcel.java:1476)
11-04 03:00:47.350: E/AndroidRuntime(642): at android.os.Parcel.readValue(Parcel.java:1810)
11-04 03:00:47.350: E/AndroidRuntime(642): at android.os.Parcel.readListInternal(Parcel.java:2017)
11-04 03:00:47.350: E/AndroidRuntime(642): at android.os.Parcel.readArrayList(Parcel.java:1461)
11-04 03:00:47.350: E/AndroidRuntime(642): at android.os.Parcel.readValue(Parcel.java:1792)
11-04 03:00:47.350: E/AndroidRuntime(642): at android.os.Parcel.readMapInternal(Parcel.java:2008)
11-04 03:00:47.350: E/AndroidRuntime(642): at android.os.Bundle.unparcel(Bundle.java:208)
11-04 03:00:47.350: E/AndroidRuntime(642): at android.os.Bundle.getDoubleArray(Bundle.java:1431)
11-04 03:00:47.350: E/AndroidRuntime(642): at alij.ne.jp.GoogleMapActivity.onCreate(GoogleMapActivity.java:111)
11-04 03:00:47.350: E/AndroidRuntime(642): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-04 03:00:47.350: E/AndroidRuntime(642): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-04 03:00:47.350: E/AndroidRuntime(642): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-04 03:00:47.350: E/AndroidRuntime(642): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-04 03:00:47.350: E/AndroidRuntime(642): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-04 03:00:47.350: E/AndroidRuntime(642): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 03:00:47.350: E/AndroidRuntime(642): at android.os.Looper.loop(Looper.java:123)
11-04 03:00:47.350: E/AndroidRuntime(642): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-04 03:00:47.350: E/AndroidRuntime(642): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 03:00:47.350: E/AndroidRuntime(642): at java.lang.reflect.Method.invoke(Method.java:521)
11-04 03:00:47.350: E/AndroidRuntime(642): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-04 03:00:47.350: E/AndroidRuntime(642): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-04 03:00:47.350: E/AndroidRuntime(642): at dalvik.system.NativeStart.main(Native Method)
11-04 03:03:10.460: I/Process(642): Sending signal. PID: 642 SIG: 9
11-04 03:03:11.099: D/dalvikvm(652): GC_EXTERNAL_ALLOC freed 792 objects / 61464 bytes in 48ms
有人知道这是什么问题吗?
感谢您阅读我的留言!
答案 0 :(得分:4)
您的readFromParcel和writeToParcel方法不匹配。
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.site_name);
dest.writeInt(this.category);
dest.writeDouble(this.latitude);
dest.writeDouble(this.longitude);
}
private void readFromParcel(Parcel in) {
this.id = in.readInt();
this.site_name = in.readString();
this.category = in.readInt();
this.latitude = in.readInt(); // <-------- this
this.longitude = in.readInt(); // <-------- and this
}
double使用8个字节,int在parcel上使用4个字节。一个parcel不包含类型信息(除非你使用writeValue和readValue),所以如果你读取和写入方法不匹配,将会读取一个意外的值,它会弄乱整个包裹。
如果你真的想要一个int,请使用(int)in.readDouble()而不是in.readInt()。