从ArrayList获取自定义类

时间:2016-10-11 10:44:02

标签: java android

我对Android很新,所以请温柔地对待我。我有一个扩展Object的自定义类。它被称为Word,并且通过应用程序的过程,某些单词被添加到ArrayList中。在活动结束时,此ArrayList通过Intent传递给另一个活动。在测试它时,我只是设置了两个单词,我知道它们都是在传递给新活动之前都在ArrayList中。当我进入第二个活动时,如果我只想获得第一个单词(即[get(0)]),我访问ArrayList没有问题。问题是,当我也尝试获取(1)时,它会抛出异常:" java.lang.RuntimeException:无法启动活动ComponentInfo {chase_crawford_69001.ltoj / chase_crawford_69001.ltoj.Finished}:java.lang。 ClassCastException:java.lang.String无法强制转换为chase_crawford_69001.ltoj.Word"

这就是我所拥有的:

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.
}

}  来自第一项活动:

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);
}

第二项活动:

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);}

请您帮我弄清楚为什么我无法从ArrayList获取第二项?我已经在这几个星期里一直在努力,我准备把头发拉出来。我确信这是我想念的简单事。谢谢!!!

以下是完整的错误跟踪:

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);

3 个答案:

答案 0 :(得分:0)

首先,如果您创建自己的对象模型,则不需要扩展Object。所以就像这样

public class Word {
    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;
    }

然后你可以将你的ArrayList转换为像这样的Gson类的String

String stringList = new Gson().toJson(yourList);

然后将Extras添加到Intent

intent.putExtra(EXTRA_KEY, stringList);

在第二个活动中,您可以获取stringList并将其转换为ArrayList

ArrayList<Word> list = new Gson().fromJson(br, new TypeToken<ArrayList<Word>>(){}.getType());

请注意,这是不好的做法,对性能不利,但只是简单的短代码

答案 1 :(得分:0)

首先,正如Hank Moody刚才所说,你不需要从Object扩展。也许你的错误可能出现在MissedWords方法中(你应该将它重命名为missWords以便遵循Java代码风格),因为你没有定义arraylist的类型而你需要进行转换。如果你重构这样的方法,你就不再需要施法了:

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

此外,你需要知道missWords中是否有第二个单词(你不能拥有,甚至不能拥有它们)。如果我们不知道你想要做什么,那就很难说了。

答案 2 :(得分:0)

Parcel in和Parcel out方法的类型不匹配。 包裹在:

flagged = in.readByte()!=0;

虽然包裹出来了:

out.writeValue(flagged);

他们需要同时调用相同类型的方法(值或字节) 最后,有效的代码是:

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

感谢大家的帮助。