型片段的方法,必须重写或实现超方法
问题描述:
我得到一个编译时错误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.Thats为什么我得到错误。
我不知道如何以正确的方式解决这个错误。任何人都可以帮助我解决这个问题。
编辑︰我明确需要该方法。顺便说一句如果删除覆盖,然后在运行时得到空指针异常。
答
将配置方法及其调用保留在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();
}
...
}
...
}
谢谢you.Hope休息。 – Steve 2014-11-24 06:20:01