【问题标题】:Error java.lang.String cannot be cast to java.lang.Integer错误 java.lang.String 无法转换为 java.lang.Integer
【发布时间】:2017-01-16 17:57:07
【问题描述】:

我有一个错误,即使在互联网上研究了类似的错误后我也无法理解。
我创建了一个整数的 ArrayList,然后简单地尝试使用 .get() 读取它,得到错误 Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer,即使我没有在这个 ArrayList 中使用任何字符串。

下面是部分代码:

package com.example.reixa.todolist;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class ListActivity extends Activity {

LinearLayout        linearList;
CheckBox            checkBox;
ArrayList<String>   checkList= new ArrayList<>();
ArrayList<Integer>  checkState = new ArrayList<>();
Bundle              bundle;
String              stuff;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_list);
    readItems();
    linearList = (LinearLayout) findViewById(R.id.linear_list);
    bundle = getIntent().getExtras();
    stuff = bundle.getString("name");

    for (int i = 0; i < checkList.size(); i++)
    {
        checkBox = new CheckBox(this);
        checkBox.setId(i);
        checkBox.setText(checkList.get(i));
        checkBox.setOnClickListener(getOnClickSomething(checkBox));
        linearList.addView(checkBox);
        if (checkState.get(i) == 0)  // error here
            checkBox.setChecked(!checkBox.isChecked());

    }
}

public void onAddItem(View v) {
    EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
    String itemText = etNewItem.getText().toString();
    checkList.add(itemText);
    checkState.add(0);
    etNewItem.setText("");
    writeItems();

    checkBox = new CheckBox(this);
    checkBox.setId(checkList.size());
    checkBox.setText(itemText);
    checkBox.setOnClickListener(getOnClickSomething(checkBox));
    linearList.addView(checkBox);
}

View.OnClickListener getOnClickSomething(final Button button) {
    return new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d("ON CLICK", "CheckBox ID: " + button.getId() + " Text: " + button.getText().toString());
            if (checkState.get(button.getId()) == 0)
                checkState.set(button.getId(), 1);
            else
                checkState.set(button.getId(), 0);
        }
    };
}

private void readItems() {
    File filesDir = getFilesDir();

    bundle = getIntent().getExtras();
    stuff = bundle.getString("name");

    File todoFile = new File(filesDir, stuff);
    try {
        checkList = new ArrayList<>(FileUtils.readLines(todoFile));
        checkState = new ArrayList<>(FileUtils.readLines(todoFile));
    } catch (IOException e) {
        checkList = new ArrayList<>();
        checkState = new ArrayList<>();
    }
}

private void writeItems() {
    File filesDir = getFilesDir();
    bundle = getIntent().getExtras();
    stuff = bundle.getString("name");

    File todoFile = new File(filesDir, stuff);
    try {
        FileUtils.writeLines(todoFile, checkList);
        FileUtils.writeLines(todoFile, checkState);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

有问题的 ArrayList 是 checkState,发生错误的行是 if (checkState.get(i) == 0)

【问题讨论】:

  • 发布完整的堆栈跟踪
  • 这段代码 sn-p 看起来没有任何问题。
  • 如何将数据填充到您的ArrayLists 中?
  • 我添加了完整的页面代码,很抱歉没有从一开始就这样做。
  • @EtienneMoussillac 请从您的控制台粘贴堆栈跟踪/完整错误消息。

标签: java android arraylist


【解决方案1】:

如果您遇到此异常,则意味着您以某种方式将String 加载到ArrayList checkState 中,根据您当前的代码,最可疑的部分是您加载readItems 的方法@987654327 @ 下一个:

// Here is your problem, you load it using String instead of Integer
checkState = new ArrayList<>(FileUtils.readLines(todoFile));

知道您以完全相同的方式初始化checkList,它是StringArrayList,很明显其中一个列表将包含错误类型的对象。

【讨论】:

  • 这一定是原因,但是因为checkStateList&lt;Integer&gt; 类型,而ArrayList 构造函数采用Collection&lt;? extends E&gt;(在这种情况下EInteger),它如何接受Collection&lt;String&gt; 而没有编译错误?
  • 你是对的,那部分是问题所在。我对其进行了修改以使其工作。谢谢!
  • @NicolasFilotto 那不可能。该方法来自 Apache Commons,it seems to return a List&lt;String&gt;
  • @EtienneMoussillac 您使用的是旧版本的 Commons 吗?旧版本使用原始类型List,它不是类型安全的,您应该更新。如果您使用了返回List&lt;String&gt;(应该如此)的版本,您的代码将不会编译。另外,readLines(File) method is deprecated - 你应该避免使用它。
  • 该信息在这里对您有很大帮助,因为您对返回类型感兴趣。 ;P
【解决方案2】:

你的问题在这里

checkState = new ArrayList<>(FileUtils.readLines(todoFile));

readLines() 返回List&lt;String&gt; 你的 checkState 是 List&lt;Integer&gt; 类型,因此是 ClassCastException。

【讨论】:

    【解决方案3】:

    您将 checkList 声明为字符串数组。您无法使用 == 检查字符串是否等于整数(或整数)。如果要比较它们,要么需要将 String 转为 int,要么将 int 转为 string。

    如果 checkList 包含一个字符串格式的数字,你可以这样做。

    if(Integer.parseInt(checkState.get(0)) == 0)
    

    或者如果它真的是一个字符串,那么你可以这样做

    if(checkState.get(0).equals("0"))
    

    或者如果你真的想检查列表大小,你可以这样做

    if(checkState.size() == 0)
    

    【讨论】:

    • 没有。他们正在使用checkState,这是一个ArrayList&lt;Integer&gt;
    • 刚刚意识到这一点。当他说他要进行从 String 到 int 的类转换时,我搞混了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    相关资源
    最近更新 更多