我收到编译时错误The method getLastCustomNonConfigurationInstance() of type TopRatedFragment must override or implement a supertype method
TopRatedFragment.java:
public class TopRatedFragment extends Fragment {
private CurlView mCurlView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
int index = 0;
if (getLastCustomNonConfigurationInstance() != null) {
index = (Integer) getLastCustomNonConfigurationInstance();
}
mCurlView = (CurlView)rootView.findViewById(R.id.curl);
mCurlView.setPageProvider(new PageProvider());
mCurlView.setSizeChangedObserver(new SizeChangedObserver());
mCurlView.setCurrentIndex(index);
mCurlView.setBackgroundColor(0xFF202830);
return rootView;
}
@Override
public Object getLastCustomNonConfigurationInstance() { ---> getting compile error
return mCurlView.getCurrentIndex();
}
我正在操作栏标签中进行页面卷曲。所以我只是将FragmentActivity代码发送到Fragment。getLastCustomNonConfigurationInstance()
方法属于FragmentActivity。这就是我收到错误的原因。
我不知道如何以正确的方式解决这个错误。任何人都可以帮助我。
编辑:我肯定需要那个方法。顺便说一下如果删除覆盖,那么在运行时会得到空指针异常。
答案 0 :(得分:1)
将配置方法及其调用保留在FragmentActivity
中,并创建一个接口以在Fragment
中获取/设置索引。
在Fragment
:
public class TopRatedFragment extends Fragment
{
public interface ISettings
{
public int getIndex();
public void setIndex(int index);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
...
int index = ((ISettings) getActivity()).getIndex();
...
}
...
}
在FragmentActivity
:
public class MainActivity extends FragmentActivity
implements TopRatedFragment.ISettings
{
private int mCurlViewIndex = 0;
@Override
public int getIndex()
{
return mCurlViewIndex;
}
@Override
public void setIndex(int index)
{
mCurlViewIndex = index;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
...
if (getLastCustomNonConfigurationInstance() != null)
{
mCurlViewIndex = (Integer) getLastCustomNonConfigurationInstance();
}
...
}
...
}