Nam*_*tha 6 android bundle object parcelable android-activity
我想做这个
class A extends Activity{
private class myClass{
}
myClass obj = new myClass();
intent i = new Intent();
Bundle b = new Bundle();
b.putParcelable(Constants.Settings, obj); //I get the error The method putParcelable(String, Parcelable) in the type Bundle is not applicable for the arguments (int, A.myClass)
i.setClass(getApplicationContext(),B.class);
startActivity(i);
}
Run Code Online (Sandbox Code Playgroud)
如何使用Parcelable将obj传递给活动B?
创建您的类并实现Serializable:
private class myClass implements Serializable {
}
Run Code Online (Sandbox Code Playgroud)
并喜欢:
myClass obj = new myClass();
Intent aActivity = (A.this, B.class);
intent.putExtra("object", obj);
Run Code Online (Sandbox Code Playgroud)
在接收方:
myClass myClassObject = getIntent().getSerializableExtra("object");
Run Code Online (Sandbox Code Playgroud)
正如错误所示,您需要使您的类(myClass在本例中)实现Parcelable.如果你查看文档Bundle,所有putParcelable方法Parcelable都采用某种形式的一个或一个集合.(这是有意义的,给定名称.)因此,如果你想使用该方法,你需要有一个Parcelable实例放入包...
当然,你不具备使用putParcelable-你可以实现Serializable,而不是和调用putSerializable.
| 归档时间: |
|
| 查看次数: |
20208 次 |
| 最近记录: |