简体   繁体   中英

Error java.lang.String cannot be cast to java.lang.Integer

I have an error that I don't understand even after researching similar errors on the internet.
I create an ArrayList of integer, and then simply try to read it with .get(), getting the error Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer even though I don't use any Strings with this ArrayList.

Here is the part of the code:

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();
    }
}
}

The ArrayList in question is checkState and the line where the error happens is if (checkState.get(i) == 0) .

Your problem lies here

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

readLines() returns List<String> Where as your checkState is of type List<Integer> and hence the ClassCastException.

If you get this exception it means that somehow you have loaded String into the ArrayList checkState , and according to your current code, the most suspicious part is the method readItems in which you load checkState as next:

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

Knowing that you initialize checkList which is an ArrayList of String the exact same way, it is obvious that one of the lists would contain objects of the wrong type.

You declared checkList as an array of String. You cannot check if a String equals an Integer (or int) using ==. If you want to compare them, you either need to turn the String to an int, or turn the int to a string.

If checkList contains a number in String format, you can do this.

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

Or if it really is a string then you could do this

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

Or if you were really meaning to check the list size you could do this

if(checkState.size() == 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM