活动之间传递的ArrayList [英] Passing ArrayList between activities
问题描述
我有一个活动的地方,我将对象添加到它实现了Parcelable接口的ArrayList。然后,我通过一个包这个列表传递到另一个活动,但是我得到以下错误,当我尝试打印新的活动列表的大小:
了java.lang.RuntimeException:无法恢复活动{com.example.test / com.example.test.SectionsActivity}:显示java.lang.NullPointerException
下面是我添加到列表中的第一个活动:
@覆盖
保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
Toast.makeText(getApplicationContext(),l.getItemAtPosition(位置)的ToString()+添加命令,Toast.LENGTH_SHORT).show();
//点击的项目添加到orderData ....
菜单项M =(菜单项)l.getItemAtPosition(位置);
//创建新项目
orderData.add(米);
小计+ = m.getPrice();
计算();
}
这是我的意图,通过将数据发送第一个活动:
confirmBtn.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
b.putParcelable(订单,orderData);
意图I =新的意图(v.getContext(),SectionsActivity.class);
i.putExtra(数据,B);
startActivity(ⅰ);
}
});
而这正是我试图找回包和显示尺寸次活动:
@覆盖
保护无效onResume(){
super.onResume();
getIntentData();
}
公共无效getIntentData(){
意图I = getIntent();
如果(我= NULL和放大器;!i.hasExtra(数据)){
Toast.makeText(this.getApplicationContext(),:收到,Toast.LENGTH_LONG).show();
B = i.getExtras();
orderData = b.getParcelable(订单);
INT大小= orderData.size();
Toast.makeText(this.getApplicationContext(),将String.valueOf(尺寸),Toast.LENGTH_LONG).show();
}
}
任何想法,为什么我得到空指针?它的驾驶我疯了!
orderData是MenuItemList对象:
公共类MenuItemList扩展的ArrayList<菜单项>实现Parcelable {
/ **
*
* /
私有静态最后长的serialVersionUID = 2998684930374219271L;
公共MenuItemList(){
}
公共静态最终Parcelable.Creator< MenuItemList> CREATOR =新Parcelable.Creator< MenuItemList>(){
@覆盖
公共MenuItemList createFromParcel(宗源){
返回新MenuItemList(源);
}
@覆盖
公共MenuItemList [] newArray(INT尺寸){
返回新MenuItemList【尺寸】;
}
};
公共MenuItemList(宗源){
readFromParcel(源);
}
私人无效readFromParcel(宗源){
this.clear();
//读取列表的大小
INT大小= source.readInt();
//还记得为了写进地块项目。这里重要的。
的for(int i = 0; I<大小;我++){
菜单项项=新菜单项();
item.setName(source.readString());
item.setPrice(source.readDouble());
this.add(项目);
}
}
@覆盖
公共无效writeToParcel(包裹DEST,INT标志){
INT大小= this.size();
dest.writeInt(大小);
的for(int i = 0; I<大小;我++){
菜单项项= this.get(我);
dest.writeString(item.getName());
dest.writeDouble(item.getPrice());
}
}
@覆盖
公众诠释describeContents(){
返回0;
}
}
变为code,它解决的问题:
时的onClick(视图v):
@覆盖
公共无效的onClick(视图v){
意图I =新的意图(v.getContext(),SectionsActivity.class);
i.putExtra(数据,(ArrayList中<菜单项>)orderData);
startActivity(ⅰ);
}
时getIntent():
公共无效getIntentData(){
意图I = getIntent();
如果(我= NULL和放大器;!&安培; i.hasExtra(数据)){
Toast.makeText(this.getApplicationContext(),:收到,Toast.LENGTH_LONG).show();
orderData = i.getParcelableExtra(数据);
INT大小= orderData.size();
Toast.makeText(this.getApplicationContext(),将String.valueOf(尺寸),Toast.LENGTH_LONG).show();
}
}
重写
问题是,你有一个额外的包。目前在 getIntentData()
你必须调用:
getIntent()//获取的意图,
.getExtras()//获取额外的包,
.getBundle(数据)//获取捆绑数据,
.getParcelable(命令); //然后让你的parcelable ...
让我们削减了不必要的包。
公共无效的onClick(视图v){
意图I =新的意图(v.getContext(),SectionsActivity.class);
i.putExtra(数据,orderData);
startActivity(ⅰ);
}
现在更新 getIntentData()
:
意图I = getIntent();
如果(我= NULL和放大器;!&安培; i.hasExtra(数据)){
orderData = i.getParcelableExtra(数据);
...
}
I have an activity where I am adding objects into an ArrayList which implements the Parcelable interface. I then pass this list to another activity via a bundle however I get the following error when I try to print the size of the list in the new activity:
java.lang.RuntimeException: Unable to resume activity {com.example.test/com.example.test.SectionsActivity}: java.lang.NullPointerException
Here is the first activity where I add to list:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getApplicationContext(), l.getItemAtPosition(position).toString() + " added to order", Toast.LENGTH_SHORT).show();
// add clicked item to orderData....
MenuItem m = (MenuItem) l.getItemAtPosition(position);
// create new item
orderData.add(m);
subTotal += m.getPrice();
calc();
}
This is first activity where I send the data via intent:
confirmBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b.putParcelable("order", orderData);
Intent i = new Intent(v.getContext(), SectionsActivity.class);
i.putExtra("data",b);
startActivity(i);
}
});
and this is second activity where I try to retrieve the bundle and display size:
@Override
protected void onResume() {
super.onResume();
getIntentData();
}
public void getIntentData(){
Intent i = getIntent();
if(i != null & i.hasExtra("data")){
Toast.makeText(this.getApplicationContext(), "recieved", Toast.LENGTH_LONG).show();
b = i.getExtras();
orderData = b.getParcelable("order");
int size = orderData.size();
Toast.makeText(this.getApplicationContext(), String.valueOf(size), Toast.LENGTH_LONG).show();
}
}
Any ideas why I am getting the null pointer?? It's driving me mad!
orderData is a MenuItemList object:
public class MenuItemList extends ArrayList <MenuItem> implements Parcelable{
/**
*
*/
private static final long serialVersionUID = 2998684930374219271L;
public MenuItemList(){
}
public static final Parcelable.Creator<MenuItemList> CREATOR = new Parcelable.Creator<MenuItemList>() {
@Override
public MenuItemList createFromParcel(Parcel source) {
return new MenuItemList(source);
}
@Override
public MenuItemList[] newArray(int size) {
return new MenuItemList[size];
}
};
public MenuItemList(Parcel source) {
readFromParcel(source);
}
private void readFromParcel(Parcel source) {
this.clear();
//read the size of the list
int size = source.readInt();
//Remember order of items written into the Parcel. Important here.
for(int i = 0; i < size; i ++){
MenuItem item = new MenuItem();
item.setName(source.readString());
item.setPrice(source.readDouble());
this.add(item);
}
}
@Override
public void writeToParcel(Parcel dest, int flags) {
int size = this.size();
dest.writeInt(size);
for(int i = 0; i < size; i ++){
MenuItem item = this.get(i);
dest.writeString(item.getName());
dest.writeDouble(item.getPrice());
}
}
@Override
public int describeContents() {
return 0;
}
}
CHANGES TO CODE THAT SOLVED PROBLEM:
CHANGED onClick(View v):
@Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(), SectionsActivity.class);
i.putExtra("data", (ArrayList<MenuItem>)orderData);
startActivity(i);
}
CHANGED getIntent():
public void getIntentData(){
Intent i = getIntent();
if(i != null && i.hasExtra("data")){
Toast.makeText(this.getApplicationContext(), "recieved", Toast.LENGTH_LONG).show();
orderData = i.getParcelableExtra("data");
int size = orderData.size();
Toast.makeText(this.getApplicationContext(), String.valueOf(size), Toast.LENGTH_LONG).show();
}
}
Rewrite
The problem is that you have an extra Bundle. Currently in getIntentData()
you have to call:
getIntent() // Fetch the Intent,
.getExtras() // Fetch the Bundle of extras,
.getBundle("data") // Fetch the Bundle "data",
.getParcelable("order"); // Then get your parcelable...
Let's cut out the unnecessary Bundle.
public void onClick(View v) {
Intent i = new Intent(v.getContext(), SectionsActivity.class);
i.putExtra("data", orderData);
startActivity(i);
}
Now update getIntentData()
:
Intent i = getIntent();
if(i != null && i.hasExtra("data")){
orderData = i.getParcelableExtra("data");
...
}
这篇关于活动之间传递的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!