-1

我正在尝试在片段被销毁之前将值保存在我的首选项中,但是一条错误消息说

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();

    }
}

当我评论循环时,一切正常!

4

2 回答 2

4

只需保持循环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();
    }
于 2019-09-21T09:42:59.027 回答
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();
        

    }
}
于 2019-09-21T09:42:15.393 回答