基本上,我在viewpager中有三个片段来创建一个选项卡式活动。我的主活动中有一个名为“ SecondActivity”的按钮“ GotoCart”。该按钮的工作原理是:单击该按钮时,它将移至名为“ Cart”的新活动。在购物车中,我有一个ListView,它从每个Fragment中分别存在的三个不同的ArrayList中获取数据。现在的问题是,当我先单击“ GoToCart”按钮时,两个片段ArrayList被初始化并将其数据放入FinalList中,但显示错误对于Screen Shot of error的第三个片段,“ arrayList未初始化”。为前两个片段工作,但不为第三个片段工作,我不明白这是什么问题。请帮忙!!!。以下是第三个片段.java文件。
public class FragSandwitch extends Fragment implements View.OnClickListener,Serializable{
Button btnadd1,btnadd2,btnadd3,btnadd4,btnadd5;
static ArrayList<SingleCartItem> sandwichList;
String x,y;
TextView itemName,price;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
final View myView = inflater.inflate(R.layout.frag_sandwich, container, false);
btnadd1=(Button)myView.findViewById(R.id.sandwich1_btn_add);
btnadd2=(Button)myView.findViewById(R.id.sandwich2_btn_add);
btnadd3=(Button)myView.findViewById(R.id.sandwich3_btn_add);
btnadd4=(Button)myView.findViewById(R.id.sandwich4_btn_add);
btnadd5=(Button)myView.findViewById(R.id.sandwich5_btn_add);
sandwichList = new ArrayList<SingleCartItem>();
btnadd1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(btnadd1.getText().toString().equals("+ADD")) {
itemName = (TextView) myView.findViewById(R.id.sandwich1_txt_name);
price = (TextView) myView.findViewById(R.id.sandwich1_txt_price);
x = (String) itemName.getText();
y = (String) price.getText();
sandwichList.add(new SingleCartItem(x, y));
btnadd1.setText("Added to Cart");
}
}
});
btnadd2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (btnadd2.getText().toString().equals("+ADD")) {
itemName = (TextView) myView.findViewById(R.id.sandwich2_txt_name);
price = (TextView) myView.findViewById(R.id.sandwich2_txt_price);
x = (String) itemName.getText();
y = (String) price.getText();
sandwichList.add(new SingleCartItem(x, y));
btnadd2.setText("Added to Cart");
}
}
});
btnadd3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (btnadd3.getText().toString().equals("+ADD")) {
itemName = (TextView) myView.findViewById(R.id.sandwich3_txt_name);
price = (TextView) myView.findViewById(R.id.sandwich3_txt_price);
x = (String) itemName.getText();
y = (String) price.getText();
sandwichList.add(new SingleCartItem(x, y));
btnadd3.setText("Added to Cart");
}
}
});
btnadd4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (btnadd4.getText().toString().equals("+ADD")){
itemName = (TextView) myView.findViewById(R.id.sandwich4_txt_name);
price = (TextView) myView.findViewById(R.id.sandwich4_txt_price);
x = (String) itemName.getText();
y = (String) price.getText();
sandwichList.add(new SingleCartItem(x, y));
btnadd4.setText("Added to Cart");
}
}
});
btnadd5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(btnadd5.getText().toString().equals("+ADD")){
itemName = (TextView) myView.findViewById(R.id.sandwich5_txt_name);
price = (TextView) myView.findViewById(R.id.sandwich5_txt_price);
x=(String)itemName.getText();
y=(String)price.getText();
sandwichList.add(new SingleCartItem(x,y));
btnadd5.setText("Added to Cart");
}
}
});
return myView;
}
@Override
public void onClick(View v) {
}
这是“ GoToCart”按钮的“ onClick”的“ SecondActivity”代码。
public void goToCart(View view) {
for (int i=0;i<FragBurger.burgerList.size();i++)//working
finalList.add(FragBurger.burgerList.get(i));
for (int i=0;i<FragPizza.pizzaList.size();i++)//working
finalList.add(FragPizza.pizzaList.get(i));
for (int i=0;i<FragSandwitch.sandwichList.size();i++)//Error
finalList.add(FragSandwitch.sandwichList.get(i));
Intent goToCart=new Intent(SecondActivity.this,Cart.class);
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)finalList);
goToCart.putExtra("BUNDLE",args);
startActivity(goToCart);
}
“购物车”活动:
public class Cart extends AppCompatActivity implements View.OnClickListener{
ArrayList<SingleCartItem> arrayList;
ListView listView;
TextView quantity;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cart);
getSupportActionBar().setTitle("Cart");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
listView=(ListView)findViewById(R.id.added_list_items);
Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");
arrayList = (ArrayList<SingleCartItem>) args.getSerializable("ARRAYLIST");
MyAdapterView my_adapter_view=new MyAdapterView( getApplicationContext(),arrayList);
my_adapter_view.notifyDataSetChanged();
listView.setAdapter(my_adapter_view);
args.remove("BUNDLE");}}