ssummeraw 2013-08-05 08:07 采纳率: 10%
浏览 2782

为什么会出现 ClassCastException 异常?

我想把ListView中的内容发送到另一个Activity中。
但是在这一行. .putStringArrayListExtra("list", (ArrayList) listItems)); 获得ClassCastException 异常。
如何处理这个错误?

private double overallTotalproduct;
public static TextView resultTextView;
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
private Button whateverButton;
   TextView inputPrice;
   RelativeLayout lay1;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menuview);
    adapter=new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            listItems);
            setListAdapter(adapter);
whateverButton = (Button)findViewById(R.id.whateverButton);
inputPrice= (TextView)findViewById(R.id.inputPrice);
lay1= (RelativeLayout)findViewById(R.id.lay1);
//Total Box
final TextView textViewtotalproduct = (TextView) findViewById(R.id.inputPrice);
final TextView textView1 = (TextView) findViewById(R.id.m1aa);
final String stringm1aa = textView1.getText().toString();
final double intm1aa = Double.parseDouble(stringm1aa);
final TextView textView1a = (TextView) findViewById(R.id.m1a);
final String stringm1a = textView1a.getText().toString();
  lay1.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        listItems.add("$"+intm1aa +"-"+ stringm1a);
        adapter.notifyDataSetChanged();
        resultTextView.setText(stringm1a);
        overallTotalproduct =  intm1aa + overallTotalproduct;
        textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
    }
});
  public void onwhateverPress(View v) {
  Intent whateverIntent = new Intent(this, WhateverActivity.class);
 if (whateverResult.iswhateveranicewhatever()) {
 final TextView daplane =(TextView)findViewById(R.id.date);
 String watch = daplane.getText().toString();
 startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",(CharSequence)watch)

//THIS IS THE LINE IM GETTING THE ERROR ON
.putStringArrayListExtra("list", (ArrayList<String>) listItems));
finish();
And the Second Activity
Intent id11 = getIntent();
if (id11.getCharSequenceExtra("list") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleNotes);
setmsg.setText(id11.getCharSequenceExtra("list"));              
}
  • 写回答

1条回答

  • 码密码密哄 2013-08-05 08:56
    关注

    顾名思义类型转换异常 有可能此时你的ArrayList还是null

    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效