2013-07-18 34 views
9

Trước hết tôi có kiểm tra this answer.Làm cách nào để mở rộng lớp học android thực hiện giao diện Parcelable?

Điều tôi đang cố gắng thực hiện là mở rộng Location lớp gọi số LocationPlus trong đó có một số biến số thành viên. chức năng tôi đang cố gắng đạt được là vượt qua đối tượng của lớp LocationPlus từ hoạt động này sang hoạt động khác.

Đây là CREATOR

public static final Parcelable.Creator<LocationPlus> CREATOR = new Parcelable.Creator<LocationPlus>() { 
    @Override 
    public LocationPlus createFromParcel(Parcel source) { 
     return new LocationPlus(source); 
    } 
    @Override 
    public LocationPlus[] newArray(int size) { 
     return new LocationPlus[size]; 
    } 
}; 

vấn đề của tôi tôi đang phải đối mặt là lỗi này

Implicit super constructor Location() is undefined. Must explicitly invoke another constructor 

khi cố gắng viết constructor

public LocationPlus(Parcel in) { 

Someone in comment yêu cầu tôi gửi lớp LocationPlus ở đây là

public class LocationPlus extends Location{ 

    private int mBattery = -1; 

    public LocationPlus(String locationName) { 
     super(locationName); 
    } 

    public LocationPlus(Location location) { 
     super(location); 
    } 

    public int getmBattery() { 
     return mBattery; 
    } 

    public void setmBattery(int mBattery) { 
     this.mBattery = mBattery; 
    } 
    @Override 
    public int describeContents() { 
     return 0; 
    } 

    public static final Parcelable.Creator<LocationPlus> CREATOR = new Parcelable.Creator<LocationPlus>() { 
     @Override 
     public LocationPlus createFromParcel(Parcel source) { 
      return new LocationPlus(source); 
     } 
     @Override 
     public LocationPlus[] newArray(int size) { 
      return new LocationPlus[size]; 
     } 
    }; 

    @Override 
    public void writeToParcel(Parcel out, int flags) { 
     super.writeToParcel(out, flags); 
     out.writeInt(mBattery); 
    } 

    public LocationPlus(Parcel in) { 
     mBattery =in.readInt(); 
    } 
} 
+0

bài LocationPlus lớp –

+0

đang @Hasslarn của bạn được đăng. – Akram

+0

Chỉ để được tò mò, nó nói gì nếu bạn đặt một constructor rỗng trong LocationPlus (thử cả hai trống rỗng và với super call): public LocationPlus() {super()}. Tôi không có Java trên máy này vì vậy tiếc là tôi không thể thử nó. Trình tạo vị trí có thể được bảo vệ. –

Trả lời

0

Theo tài liệu Android, không có một hàm tạo Location() cho lớp Vị trí. Khi khởi tạo lớp học LocationPlus, bạn cần gọi số super(String provider) hoặc super(Location l).

Edit: Corrected cú pháp

(Xem Location Android Doc)

+0

Cảm ơn thời gian và câu trả lời quý giá của bạn Brian tôi đã thấy Tài liệu tôi đang tìm kiếm bất kỳ công việc nào xung quanh cho vấn đề này. – Akram

+0

Những gì bạn cần làm là bao gồm một cuộc gọi đến một nhà cung cấp 'super (String provider)' hoặc 'super (Location l)' trong phương thức 'LocationPlus (Parcel in)'. – Brian

1

Hãy thử giải pháp này:

public static final Parcelable.Creator<LocationPlus> CREATOR = 
    new Parcelable.Creator<LocationPlus>() { 
    @Override 
    public LocationPlus createFromParcel(Parcel in) { 

     Location l = Location.CREATOR.createFromParcel(in); 
     LocationPlus lp = new LocationPlus(l); 

     lp.mBattery= in.readInt(); 

     return lp; 
    } 

    @Override 
    public LocationPlus[] newArray(int size) { 
     return new LocationPlus[size]; 
    } 
}; 

@Override 
public void writeToParcel(Parcel parcel, int flags) { 
    super.writeToParcel(parcel, flags); 
    parcel.writeInt(mBattery); 

} 
4

Hi Tôi đã làm nghiên cứu rất nhiều về điều này, nhưng tôi không thể tìm thấy hữu ích bất cứ điều gì. Tôi cố gắng giải pháp dưới đây và nó đã làm việc cho tôi.

Giả sử lớp học của bạn chỉ có tên biến là "mData".

public class Location implements Parcelable { 
protected int mData; 

public int describeContents() { 
    return 0; 
} 

public void writeToParcel(Parcel out, int flags) { 
    out.writeInt(mData); 
} 

public static final Parcelable.Creator<Location> CREATOR 
     = new Parcelable.Creator<Location>() { 
    public Location createFromParcel(Parcel in) { 
     return new Location(in); 
    } 

    public Location[] newArray(int size) { 
     return new Location[size]; 
    } 
}; 

private Location(Parcel in) { 
    mData = in.readInt(); 
} 

}

Sau đó, lớp mở rộng của bạn chỉ có int biến có tên là "mBattery".

public class LocationPlus extends Location { 
protected int mBattery; 

public int describeContents() { 
    return 0; 
} 

public void writeToParcel(Parcel out, int flags) { 
    out.writeInt(mBattery); 
} 

public static final Parcelable.Creator<LocationPlus> CREATOR 
     = new Parcelable.Creator<LocationPlus>() { 
    public LocationPlus createFromParcel(Parcel in) { 
     return new LocationPlus(in); 
    } 

    public LocationPlus[] newArray(int size) { 
     return new LocationPlus[size]; 
    } 
}; 

private LocationPlus(Parcel in) { 
    mBattery = in.readInt(); 
} 

}

Cho đến nay, LocationPlus hoạt động tốt. Nhưng chúng tôi không đặt biến của lớp siêu. Thứ nhất, tôi đặt các biến của lớp siêu trên lớp mở rộng với phương thức siêu (..). Nhưng nó không hoạt động.

private LocationPlus(Parcel in) { 
    super(in); 
    mBattery = in.readInt(); 
} 

Thay vì mã ở trên, bạn nên đặt tất cả biến siêu lớp một cách rõ ràng. Các biến của lớp siêu nên được bảo vệ.constructor thức nên như thế này:

private LocationPlus(Parcel in) { 
    mData = in.readIn(); 
    mBattery = in.readInt(); 
} 

và phương pháp writeToParcel nên như thế này:

public void writeToParcel(Parcel out, int flags) { 
    out.writeIn(mData); 
    out.writeInt(mBattery); 
} 
+0

Làm siêu (in) trong trình xây dựng LocationPlus nên hoạt động, bạn chỉ cần đảm bảo cũng làm siêu.writeToParcel (ra, cờ) trong writeToParcel. – SilentByte

28

Parcelable, Vua Tốc độ

Theo google engineers, mã này sẽ chạy nhanh hơn đáng kể. Một trong những lý do cho điều này là chúng ta đang rõ ràng về quá trình tuần tự hóa thay vì sử dụng sự phản chiếu để suy ra nó. Nó cũng là lý do mà mã đã được tối ưu hóa rất nhiều cho mục đích này.

public abstract class BaseClass implements Parcelable { 

    public String FullName; 
    public boolean IsValidUser; 
    public String UserName; 


    public BaseClass() { 
    } 


    protected BaseClass(Parcel in) { 
     FullName = in.readString(); 
     IsValidUser = in.readByte() != 0; 
     UserName = in.readString(); 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(FullName); 
     dest.writeByte((byte) (IsValidUser ? 1 : 0)); 
     dest.writeString(UserName); 
    } 
} 

lớp trẻ sẽ như sau với việc sử dụng của danh sách thêm vào đối tượng parcelable:

public class DerivedClass extends BaseClass { 

    public boolean IsSuccess; 
    public String Message; 
    public List<AnotherClass> AnotherClassObj; 


    public DerivedClass() { 
     super(); 
    } 

    protected DerivedClass(Parcel in) { 
     super(in); 
     AnotherClassObj = new ArrayList<AnotherClass>(); 
     IsSuccess = in.readByte() != 0; 
     Message = in.readString(); 
     AnotherClassObj = in.readArrayList(AnotherClass.class.getClassLoader()); 
    } 

    public static final Creator<DerivedClass> CREATOR = new Creator<DerivedClass>() { 
     @Override 
     public DerivedClass createFromParcel(Parcel in) { 
      return new DerivedClass(in); 
     } 

     @Override 
     public DerivedClass[] newArray(int size) { 
      return new DerivedClass[size]; 
     } 
    }; 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     super.writeToParcel(dest, flags); 
     dest.writeByte((byte) (IsSuccess ? 1 : 0)); 
     dest.writeString(Message); 
     dest.writeList(AnotherClassObj); 
    } 

    public int describeContents() { 
     return 0; 
    } 
} 

Một con lớp:

public class AnotherClass extends BaseClass { 
    public AnotherClass() { 
     super(); 
    } 

    protected AnotherClass(Parcel in) { 
     super(in); 
    } 

    public int describeContents() { 
     return 0; 
    } 

    public static final Creator<AnotherClass> CREATOR = new Creator<AnotherClass>() { 
     @Override 
     public AnotherClass createFromParcel(Parcel in) { 
      return new AnotherClass(in); 
     } 

     @Override 
     public AnotherClass[] newArray(int size) { 
      return new AnotherClass[size]; 
     } 
    }; 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     super.writeToParcel(dest, flags); 
    } 
} 

Trong Hoạt động:

Intent intent = new Intent(LoginActivity.this, MainActivity.class); 
intent.putExtra("UserObject", parcelableObject); 
startActivity(intent); 
finish(); 

Trong hoạt động nhận:

Bundle extras = getIntent().getExtras(); 
if (extras != null) { 
     userObject = extras.getParcelable("UserObject"); 
} 
Các vấn đề liên quan