我试图通过 Parceler 接收列表项,compile 'org.parceler:parceler-api:1.1.5'
我的代码如下所示:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<NavItem> navItems = new ArrayList<>();
navItems.add(new CategoryItem("CategoryItem"));
navItems.add(new SubCatItem("SubCatItem"));
NavItemsWraper wraper = new NavItemsWraper(navItems);
Intent data = new Intent();
data.putExtra("data", Parcels.wrap(wraper));
NavItemsWraper recievedWraper = Parcels.unwrap(data.getParcelableExtra("data"));
recievedWraper.getNavItems();
for (NavItem item:recievedWraper.getNavItems()){
Log.e(TAG, "onCreate: "+item.title);
}
}
类:
导航项
@Parcel
public abstract class NavItem {
public String title;
public NavItem(){
}
public NavItem(String subCatItem) {
this.title=subCatItem;
}
}
NavItemsWrapper
@Parcel
public class NavItemsWraper {
private List<NavItem> navItems;
public NavItemsWraper(){
}
public NavItemsWraper(List<NavItem> navItems) {
this.navItems=navItems;
}
public List<NavItem> getNavItems() {
return navItems;
}
}
类别项目
@Parcel
public class CategoryItem extends NavItem {
public CategoryItem(){
}
public CategoryItem(String categoryItem) {
super(categoryItem);
}
}
子类别项
@Parcel
public class SubCatItem extends NavItem {
public SubCatItem() {
}
public SubCatItem(String subCatItem) {super(subCatItem);}
}
编译错误是:
...\app\build\generated\source\apt\debug\com\test\objs\NavItem$$Parcelable.java
Error:(63, 26) error: NavItem is abstract; cannot be instantiated