最近在测试handler接受子线程发过来的Parcelable ArrayList时遇到了个问题,无论怎么试,handler 通过msg.getData().getParcelableArrayList("xxx") 返回的都是空指针,而几乎同样的代码,用intent传到另一个Activity,却可以正常取到Parcelable ArrayList,请教各位大虾,这是什么问题导致的?
public void click(View view) {
Bundle bundle = new Bundle() ;
bundle.putParcelableArrayList("ppp", (ArrayList<Parcelable>)users) ;
MyThread mt = new MyThread() ;
Thread t = new Thread(mt) ;
t.start() ;
}
private class MyThread implements Runnable {
@Override
public void run() {
users = new ArrayList<Parcelable>() ;
for(int i=0;i<10;i++) {
User u = new User() ;
u.setUsername(i+"") ;
u.setPassword(i+"") ;
users.add(u) ;
}
Message msg = new Message() ;
msg.what = 1 ;
Bundle bundle = new Bundle() ;
bundle.putParcelableArrayList("ppp", (ArrayList<? extends Parcelable>) users) ;
handler.sendMessage(msg) ;
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
List<Parcelable> lists = msg.getData().getParcelableArrayList("ppp") ;
System.out.println("--------------" + lists.size());
}
} ;
------解决方案--------------------
错了,应该是
Bundle?bundle?=?new?Bundle()?;
bundle.putParcelableArrayList("ppp",?(ArrayList<??extends?Parcelable>)?users)?;
msg.setData(bundle);