我有一个测验应用程序,它将多项选择题(TextView,下面有5个RadioButtons选项)作为片段显示在ViewPager中的FragmentStatePagerAdapter中。
我得到的错误只有在片段恢复时才会出现。向前看,它完美地加载了每个问题和所有5个选项。但是,当它恢复时,加载完全错误,所有5个RadioButtons显示相同的文本,特别是最后一个RadioButton的文本。我不知道为什么会发生这个错误,但我认为这可能与调用超类有关,但这只是一种预感。
代码如下:
OnCreateView按照logcat确定的方式正确设置文本:
for (String s : aArray) {
// Runs through a string array of the 5 answers
LinearLayout l = lArray.get(pos);
RadioButton r = (RadioButton) l.findViewById(R.id.radio);
Log.d(s, correctanswer);
//Log tag shows that the choices during initial creation or **restore** are the different 5 answers
r.setText(s);
}
return rootView;这是我的OnViewStateRestored
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
for (LinearLayout l : lArray) {
RadioButton r = (RadioButton) l.findViewById(R.id.radio);
Log.e(r.getText().toString(), correctanswer);
//Suddenly the displayed text is the same for all 5 buttons
if (r.isChecked())
r.performClick();
//android automatically saves check status. for full restore I click all the buttons
}
}如果您需要更多来自OnCreateView的代码或其他任何东西,请让我知道。我似乎找不出是什么导致了这个问题,并提前感谢您的帮助!
发布于 2013-02-20 03:50:37
你的每个单选按钮应该有不同的id,而不是它们都有相同的id。这就是单选按钮和其他视图通过id保存状态的方式。更准确地说,它们将其状态保存在由其id映射的SparseArray中。这就是为什么他们都有关于恢复的第五个的信息。
https://stackoverflow.com/questions/14966006
复制相似问题