ArrayList的Android类Cast Exception

时间:2014-08-29 19:50:08

标签: android class exception android-intent

我创建了一个extends ArrayList<OtherClass>的类。然后我把该类的一个实例作为额外的Intent,如intent.putExtra("questionlist", questionsList);在其他Activity中,我得到了额外的questionsList = (QuestionList) getIntent().getSerializableExtra("questionlist");并按照代码中的显示进行了抛出,但它会弹出ClassCastException. What is problem here. Thanks.

public void startTest(){
    Intent intent = new Intent("com.example.testznanja.TESTACTIVITY");
    intent.putExtra("player", player);
    intent.putExtra("numberofquestions", numberOfQuestions);
    intent.putExtra("numberofmistakes", numberOfMistakes);
    intent.putExtra("questionlist", questionsList);
    startActivity(intent);
}



private void initialize() {
    playerList = new PlayerList(this.getCacheDir());
    questionText = (TextView) findViewById(R.id.questionText);
    questionText.setGravity(Gravity.CENTER_HORIZONTAL);
    correctAnswers = (TextView) findViewById(R.id.questionsAnswers);
    b1 = (Button) findViewById(R.id.beginTest);
    b2 = (Button) findViewById(R.id.button2);
    b3 = (Button) findViewById(R.id.button3);
    b4 = (Button) findViewById(R.id.button4);
    timer = (TextView) findViewById(R.id.timer);
    testPlayer = (Player) getIntent().getSerializableExtra("key");
    numberOfQuestions = getIntent().getIntExtra("numberofquestions", 0);
    numberOfMistakes = getIntent().getIntExtra("numberofanswers", 0);
    questionsList =  (QuestionList) getIntent().getSerializableExtra("questionlist");
    questionsList.shrinkToRandomQuestions(numberOfQuestions);
}




public class QuestionList extends ArrayList<Question>{

private Context context;
private ArrayList<String> answersList = new ArrayList<String>();

public QuestionList(Context c) {
    context = c;
}

}

1 个答案:

答案 0 :(得分:0)

OtherClass必须为Serializable,才能将其作为Serializable额外内容传递给意图。仅ArrayList<?> Serializable是不够的。