Stephen
Stephen

Reputation: 10059

The method of type Fragment must override or implement a supertype method

I am getting a compile time error 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();
    }

I am doing a page curl in action bar tabs.So I just exhange the FragmentActivity codes to Fragment.getLastCustomNonConfigurationInstance() method belongs to FragmentActivity.Thats why I am getting the error.

I didn't know how to solve this error in the right way.Anybody can help me with this.

Edit: I need that method definitely.By the way If remove the override then am getting the null pointer exception at runtime.

Upvotes: 0

Views: 2212

Answers (1)

Mike M.
Mike M.

Reputation: 39191

Leave the configuration methods and their calls in the FragmentActivity, and create an interface to get/set the index there in the Fragment.

In the 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();
        ...
    }
    ...
}

And in the 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();
        }
        ...
    }
    ...
}

Upvotes: 1

Related Questions