我正在尝试在片段被销毁之前将值保存在我的首选项中,但是出现一条错误消息
android.support.v4.app.SuperNotCalledException:片段未调用到super.onDestroyView()
这是我的代码
@Override
public void onDestroyView() {
pref3.edit().clear().commit();
for (int i = 0; i < movies1.size(); i++) {
favouritemovies1.add(movies1.get(i));
SharedPreferences.Editor editor3 = pref3.edit();
editor3.putStringSet("favouritemovies", favouritemovies1);
editor3.commit();
Toast.makeText(getActivity(), "destroyed", Toast.LENGTH_SHORT).show();
super.onDestroyView();
}
}
当我为循环评论时,一切正常!
答案 0 :(得分:3)
只需将super.onDestroyView();
移出for
循环并放在@Override
方法内,如下所示:-
@Override
public void onDestroyView() {
// or here
pref3.edit().clear().commit();
for (int i = 0; i < movies1.size(); i++) {
favouritemovies1.add(movies1.get(i));
SharedPreferences.Editor editor3 = pref3.edit();
editor3.putStringSet("favouritemovies", favouritemovies1);
editor3.commit();
Toast.makeText(getActivity(), "destroyed", Toast.LENGTH_SHORT).show();
}
super.onDestroyView();
}
解决您的问题。快乐的编码
答案 1 :(得分:2)
从循环中删除 super.onDestroyView();
并将其写在顶部,如下所示。
@Override
public void onDestroyView() {
super.onDestroyView();
pref3.edit().clear().commit();
for (int i = 0; i < movies1.size(); i++) {
favouritemovies1.add(movies1.get(i));
SharedPreferences.Editor editor3 = pref3.edit();
editor3.putStringSet("favouritemovies", favouritemovies1);
editor3.commit();
Toast.makeText(getActivity(), "destroyed", Toast.LENGTH_SHORT).show();
}
}
希望它对您有帮助。 快乐编码