我有一个错误,即使在研究互联网上的类似错误后,我也不明白。
我创建了一个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)
。
Post full stacktrace – Sergey
看起来这段代码段没有任何问题。 – Ryan
如何将数据填充到ArrayList中? –