2017-01-16 93 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。

相關問題