我已经开始为Android编写一个小型应用程序。看起来很不错,但是有一件事我不明白。我在 Activity A中创建了一个新的意图,并添加了序列化的矢量intent.putExtra("key", vector)。在被调用的 Activity B上,我正在这样做:Vector<ItemModel> items = (Vector<ItemModel>) getIntent().getExtras().getSerializable("key")。这行导致ClassCastException。有人知道为什么吗?请帮我 :-)这是这两个 Activity 和ItemModel的来源:ItemModel:public class ItemModel implements java.io.Serializable { private static final long serialVersionUID = -1772392719565658730L; public int id; public String name; public String quantity; public int status;}ShoppingListActivity(A): public void onClickItems(final View v){ final Intent intent = new Intent(this,ItemListActivity.class); Vector<ItemModel> v1 = new Vector<ItemModel>(); ItemModel item = new ItemModel(); item.name = "Tomaten"; item.quantity = "400g"; item.id = 12; item.status = 0; v1.add(item); item = new ItemModel(); item.name = "Bohnen"; item.quantity = "150g"; item.id = 13; item.status = 0; v1.add(item); item = new ItemModel(); item.name = "Schokolade"; item.quantity = "5 Tafeln"; item.id = 17; item.status = 0; v1.add(item); intent.putExtra("ShoppingList", v1); startActivity(intent);ItemListActivity(B): public class ItemListActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.item); ListView list = (ListView) findViewById(R.id.ITEMSLIST); final Bundle extras = getIntent().getExtras(); Vector<ItemModel> items = (Vector<ItemModel>) extras.getSerializable("ShoppingList"); ArrayList<HashMap<String, Object>> itemsList = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> itemMap = new HashMap<String, Object>();} 最佳答案 任何实现List和Serializable的东西都会在内部转换为ArrayList。因此它可能作为Vector进入,但作为ArrayList出现。在推杆和吸气杆的两端将其视为List 可能更好。通常,也不要使用Vector。所有方法都是同步的,这使其效率不如ArrayList。
10-06 09:21