Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误java.lang.String无法转换为java.lang.Integer_Java_Android_Arraylist - Fatal编程技术网

错误java.lang.String无法转换为java.lang.Integer

错误java.lang.String无法转换为java.lang.Integer,java,android,arraylist,Java,Android,Arraylist,我有一个错误,我不明白,即使在互联网上研究类似的错误。 我创建了一个integer的ArrayList,然后简单地尝试用.get读取它,得到由以下原因引起的错误:java.lang.ClassCastException:java.lang.String不能转换为java.lang.integer,即使我没有在这个ArrayList中使用任何字符串 以下是代码的一部分: package com.example.reixa.todolist; import android.app.Activity

我有一个错误,我不明白,即使在互联网上研究类似的错误。 我创建了一个integer的ArrayList,然后简单地尝试用.get读取它,得到由以下原因引起的错误:java.lang.ClassCastException:java.lang.String不能转换为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.geti==0。

您将checkList声明为字符串数组。不能使用==检查字符串是否等于整数或int。如果要比较它们,需要将字符串转换为int,或者将int转换为字符串

如果清单包含字符串格式的数字,则可以执行此操作

if(Integer.parseInt(checkState.get(0)) == 0)
或者如果它真的是一个字符串,那么你可以这样做

if(checkState.get(0).equals("0"))
if(checkState.size() == 0)
或者如果你真的想检查列表的大小,你可以这样做

if(checkState.get(0).equals("0"))
if(checkState.size() == 0)
你的问题就在这里

checkState = new ArrayList<>(FileUtils.readLines(todoFile));
readLines返回列表
其中,由于您的checkState是List类型,因此是ClassCastException。

如果出现此异常,则意味着您以某种方式将字符串加载到ArrayList checkState中,并且根据您当前的代码,最可疑的部分是将checkState加载为next的方法readItems:


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

Post full stacktraceThere此代码段看起来没有任何问题。如何将数据填充到ArrayList中?我添加了完整的页面代码,很抱歉没有从一开始就执行此操作。@EtienMousSillac请从控制台粘贴堆栈跟踪/完整错误消息。否。他们正在使用checkState,这是一个ArrayList。刚刚意识到。当他说他要将一个类从String强制转换为int时,我搞混了。这一定是原因,但是由于checkState是List类型,而ArrayList构造函数采用CollectionYou是对的,这部分就是问题所在。我对它进行了修改以使其正常工作。谢谢@尼古拉斯·菲洛托,那不可能。该方法来自Apache Commons,并且@EtienneMoussillac您使用的是旧版本的Commons吗?旧版本使用了原始类型列表,它不是类型安全的,您应该进行更新。若您使用了一个返回列表的版本,那个么您的代码就不会编译。此外,你应该避免使用它。这些信息在这里会对你有很大帮助,因为你对退货类型感兴趣;P