【发布时间】:2014-10-23 21:03:49
【问题描述】:
我创建了一个 extends ArrayList<OtherClass> 的类。然后我将该类的一个实例作为 Intent 的额外实例,例如 intent.putExtra("questionlist", questionsList);In other 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;
}
}
【问题讨论】:
-
以简洁易读的格式发布代码。那是一团糟。
-
发布 QuestionList 的定义和实际的堆栈跟踪。另外,为什么需要扩展 ArrayList?
-
对不起,我现在发布代码。
-
哦我想起来了,作为 QuestionList 类中的一个变量,我有 Context,这可能是原因吗?
-
您也应该发布问题类定义,但我怀疑它不可序列化。
标签: android class exception android-intent