ChaseC ChaseC - 1 month ago 8
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;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(vocabWord);
out.writeString(definition);
out.writeValue(flagged);
// 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!");
view.setBackgroundColor(getResources().getColor(R.color.red));
if (!missedWords.contains(correctWord)) {
missedWords.add(correctWord);}

public void FinalMessage(View view) {

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


the second activity:

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


MissedWords(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(R.id.finished);
TextView finishedOne = (TextView) findViewById(R.id.finished1);
String wrongList = sWordOne;// + " " +
String wrongList1 = sWordTwo;
finished.setText(wrongList);
finishedOne.setText(wrongList1);}


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.app.ActivityThread.performLaunchActivity(ActivityThread.java:2080)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105)
at android.app.ActivityThread.access$600(ActivityThread.java:133)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4795)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
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(Finished.java:30)
at chase_crawford_69001.ltoj.Finished.onCreate(Finished.java:25)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2044)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105) 
at android.app.ActivityThread.access$600(ActivityThread.java:133) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4795) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
at dalvik.system.NativeStart.main(Native Method) 

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

Answer

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

flagged = in.readByte()!=0;

while Parcel out has:

out.writeValue(flagged);

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.

Comments