我正在尝试通过意图(Intent)传递一个对象的ArrayList,但是无法使其正常工作。以下是我的代码:
public class QuestionView extends Activity {
//variables and other methods...
public void endQuiz() {
Intent intent = new Intent(QuestionView.this, Results.class);
intent.putExtra("correctAnswers", correctAnswers);
intent.putExtra("wrongAnswers", wrongAnswers);
intent.putParcelableArrayListExtra("queries", queries);
startActivity(intent);
}
}
这里正在接收Intents:
public class Results extends Activity {
int cAnswers;
int wAnswers;
ArrayList<Question> qs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultsmain);
cAnswers = getIntent().getIntExtra("correctAnswers", -1);
wAnswers = getIntent().getIntExtra("wrongAnswers", -1);
qs = getIntent().getParcelableArrayListExtra("queries");
//more code...
}
}
这两个int类型的变量correctAnswer和wrongAnswers已经接收并可以使用。但是ArrayList没有传递过来,虽然endQuiz()方法没有错误,但是'qs = getIntent().getParcelableArrayListExtra("queries");'语句出错并显示“Bound Mismatch”错误。
如有帮助,请提供支持!
问题类:
public class Question {
String a1;
String a2;
String a3;
String a4;
int correctAnswer;
String query;
int selectedAnswer;
boolean correctness;
public Question() {
}
public Question(String a1, String a2, String a3, String a4, int correctAnswer, String query, int selectedAnswer, boolean correctness) {
this.a1 = a1;
this.a2 = a2;
this.a3 = a3;
this.a4 = a4;
this.correctAnswer = correctAnswer;
this.query = query;
this.selectedAnswer = selectedAnswer;
this.correctness = correctness;
}
}
Question
类实现了 Parcelable 接口吗? - wsanville(ArrayList<? extends Parcelable>) queries);
:),这肯定不会有帮助。” - SelvinArrayList<Object> a = null; ArrayList<? extends Parcelable> b = (ArrayList<? extends Parcelable>) a;
可以工作...只有警告Unchecked cast from ArrayList<Object> to ArrayList<? extends Parcelable>
...在Java中,在运行时你不知道泛型类型。 - Selvin