ChaseC ChaseC - 1 year ago 55
Android Question

Getting custom class from ArrayList

I am pretty new to Android, so please be gentle with me. I have a custom class that extends Object. It is called Word and through the course of the app, certain Words are added to an ArrayList. At the end of the activity, this ArrayList is passed via an Intent to another activity. In testing it, I just have it set up to take two Words and I know that they are both in the ArrayList before being passed to the new activity. When I get to the second activity, I have no problem accessing the ArrayList if I am only trying to get the 1st Word (ie [get(0)]). The problem is that when I also try to get (1), it throws an exception: "java.lang.RuntimeException: Unable to start activity ComponentInfo{chase_crawford_69001.ltoj/chase_crawford_69001.ltoj.Finished}: java.lang.ClassCastException: java.lang.String cannot be cast to chase_crawford_69001.ltoj.Word"

Here is what I have:

public class Word extends Object implements Parcelable{
private String vocabWord;
private String definition;
private Boolean flagged = Boolean.FALSE;

public Word (String vocabWord, String definition) {
this.vocabWord = vocabWord;
this.definition = definition;}
public Word (String vocabWord){ //Used to make ten Language words so that all TextViews are full,
// but this will never be the correct answer.
this.vocabWord = vocabWord;}

public String getVocabWord(){return vocabWord;}
public String getDefinition(){return definition;}
public Boolean getFlagged(){return flagged;}

public void setVocabWord(String vocabWord) {this.vocabWord = vocabWord;}
public void setDefinition(String definition) {this.definition = definition;}
public void setFlag(Boolean used){this.flagged = used;}

public int describeContents() {
return 0;
public void writeToParcel(Parcel out, int flags) {
// out.writeByte((byte) (flagged ? 1:0));//1 == TRUE, 0== FALSE

public static final Parcelable.Creator<Word> CREATOR
= new Parcelable.Creator<Word>() {
public Word createFromParcel(Parcel in) {
return new Word(in);

public Word[] newArray(int size) {
return new Word[size];

private Word(Parcel in) {
vocabWord = in.readString();
definition = in.readString();
flagged = in.readByte()!=0;//If not 0, flagged is true.

from the 1st activity:

ArrayList<Word> missedWords = new ArrayList<>(); //this was instantiated at the beginning of the activity

else {
view.setText("Try Again!");
if (!missedWords.contains(correctWord)) {

public void FinalMessage(View view) {

Intent toFinished = new Intent(this, Finished.class);
missedWordsBundle.putParcelableArrayList("missedWords", missedWords);
toFinished.putExtra("missedWordsList", missedWordsBundle);

the second activity:

protected void onCreate(Bundle savedInstanceState) {
Intent words = getIntent();
Bundle missedWordsBundle;
ArrayList<Word> missedWords = new ArrayList();
//missedWords = getIntent().getParcelableArrayListExtra("missedWords");
missedWordsBundle = this.getIntent().getBundleExtra("missedWordsList");
missedWords = missedWordsBundle.getParcelableArrayList("missedWords");


public void MissedWords(ArrayList missedWords) {
wordOne = (Word) missedWords.get(0);
wordTwo = (Word) missedWords.get(1);

String sWordOne = wordOne.getVocabWord();
String sWordTwo = wordTwo.getVocabWord();
TextView finished = (TextView) findViewById(;
TextView finishedOne = (TextView) findViewById(;
String wrongList = sWordOne;// + " " +
String wrongList1 = sWordTwo;

Would you please help me figure out why I am unable to get the second item from the ArrayList? I have been struggling with this for a couple weeks now, and am about to pull my hair out. I am sure that it is something simple that I am missing. Thank You!!!

Here is the complete error trace:

10-12 04:24:48.789 353-353/chase_crawford_69001.ltoj E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{chase_crawford_69001.ltoj/chase_crawford_69001.ltoj.Finished}: java.lang.ClassCastException: java.lang.String cannot be cast to chase_crawford_69001.ltoj.Word
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to chase_crawford_69001.ltoj.Word
at chase_crawford_69001.ltoj.Finished.missedWords(
at chase_crawford_69001.ltoj.Finished.onCreate(
at android.os.Handler.dispatchMessage( 
at android.os.Looper.loop( 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke( 
at dalvik.system.NativeStart.main(Native Method) 

Line 30 is: wordTwo = missedWords.get(1);
Line 25 is: missedWords(missedWords);

Answer Source

The Parcel in and Parcel out methods do not match in type. Parcel in has:

flagged = in.readByte()!=0;

while Parcel out has:


They need to both call the same type of method (Value or Byte) In the end the code that worked was:

out.writeByte((byte) (flagged ? 1:0));//1 == TRUE, 0== FALSE
flagged = in.readByte()!=0;//If not 0, flagged is true.

Thanks for your help everyone.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download