2014-08-29 56 views
0

我创建了一个类extends ArrayList<OtherClass>。然后我把这个类的一个实例作为意向的额外像intent.putExtra("questionlist", questionsList);在其他活动,我得到的是如图所示的代码中加入额外像questionsList = (QuestionList) getIntent().getSerializableExtra("questionlist");,并投它,但它会弹出ClassCastException. What is problem here. Thanks.Android Class CastList异常ArrayList

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

以干净可读的格式发布代码。这是一团糟。 – r2DoesInc 2014-08-29 19:51:55

+0

发布QuestionList的定义,以及实际的堆栈跟踪。另外,为什么你需要扩展ArrayList? – njzk2 2014-08-29 19:57:37

+0

对不起,我现在发布了代码。 – 2014-08-29 19:57:45

回答

0

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