【发布时间】: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 请从您的控制台粘贴堆栈跟踪/完整错误消息。