2017-01-16 311 views
0

我有一个错误,即使在研究互联网上的类似错误后,我也不明白。
我创建了一个ArrayList的整数,然后试着用.get()读取它,即使我没有在这个ArrayList中使用任何字符串,也会得到错误Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer错误的java.lang.String不能转换为java.lang.Integer

这里是代码的一部分:

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)

+5

Post full stacktrace – Sergey

+2

看起来这段代码段没有任何问题。 – Ryan

+1

如何将数据填充到ArrayList中? –

回答

0

如果你得到这个异常就意味着在某种程度上已加载StringArrayListcheckState,并根据您当前的代码,最可疑的部分是在其中加载checkState在明年的方法readItems

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

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

+0

这一定是原因,但是由于'checkState'的类型是'List ',而'ArrayList'构造函数需要'Collection '(其中'E'在这里是'Integer'),它如何接受'集合'没有编译错误? – Moira

+1

你说得对,那部分是问题。我修改它以使其工作。谢谢! – user3794667384

+0

@NicolasFilotto那不可能。该方法来自Apache Commons,[它似乎返回'List '](https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io /FileUtils.html#readLines(java.io.File))。 – Moira

-2

您将checkList声明为String数组。你不能使用==检查一个String是否等于Integer(或int)。如果你想比较它们,你需要将字符串转换为int,或将int转换为字符串。

如果checkList包含一个String格式的数字,您可以这样做。

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

或者,如果它确实是一个字符串,那么你可以做到这一点

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

或者,如果你真的意味着检查列表的大小,你可以做到这一点

if(checkState.size() == 0) 
+1

不。他们正在使用'checkState',它是'ArrayList '。 – Moira

+0

刚刚意识到这一点。当他说他得到一个从String到int的类时,我被混淆了。 –

0

你的问题在于这里

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

readLines()返回List<String> 您的checkState类型为List<Integer>,因此ClassCastException。

相关问题