OnCreateView not called with fragment in ViewPager

I use ViewPagerfrom the ACL to show a pair of Fragments. First these fragments are where everything ListFragmentis because they contain a list. I am trying to simulate a table with two columns, so each item in the list contains a different list of two objects. I want each cell to be long-needed (not the whole list item), so I implemented my own ColumnLayout (perhaps it should be called CellLayout). Each list item contains two ColumnLayouts columns, which are long clicks and implement getContextMenuInfo()to return information about which object was pressed long. This method scans the ViewPager, requests the current fragment, and then calls the fragment to return the object identifier from a long click. To get the correct line, you need to make a fragment:

getListView().getPositionForView(v)

And here, where the problem begins. Sometimes it getListView()displays the IllegalStateExceptionmessage "Content view has not yet been created." That is, onCreateView(...)it was not called. This happens sometimes when the application resumes. Today I was able to re-create the problem by turning on the "Do not save actions" option in the settings> Developer options on my Galaxy Nexus. I launched the application, click "Home", and then open the application again from the menu of the latest applications, and now, if I click on any of the cells in my list for a long time, this exception is thrown. Based on some debugging findings, I was able to verify that onCreateView was never called. Which is strange, because the entire ViewPager with its fragment and its ListView and its cell elements are drawn!

# android-dev, ListFragment ACL , android.support.v4.app.Fragment ListFragment, .

: , MenuInfo, entitiy . , , listview.getPositionForView(View v), IllegalStateException.

(, ViewPager, ):

package org.remotestick;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.LinearLayout;

public class ColumnLayout extends LinearLayout {

    public ColumnLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ColumnLayout(Context context) {
        super(context);
    }

    @Override
    protected ContextMenuInfo getContextMenuInfo() {
        int itemTypeId = getChildAt(0).getId();
        int positionTypeId = getId();
        View currentView = this;
        ViewPager viewPager = null;
        while(currentView.getParent() != null) {
            currentView = (View) currentView.getParent();
            if(currentView.getId() == R.id.pager) {
                viewPager = (ViewPager) currentView;
                break;
            } else if (currentView.getId() == R.id.rootLayout)
                break;
        }
        if(viewPager == null)
            return null;

        WorkspaceAdapter adapter = (WorkspaceAdapter) viewPager.getAdapter();
        Pair<Integer, Integer> itemIdAndListPosition = adapter.getItemIdFromWorkspace(viewPager.getCurrentItem(), this, itemTypeId, (positionTypeId == R.id.leftColumn));
        if(itemIdAndListPosition == null)
            return null;
        else
            return new MatrixAdaptableContextMenuInfo(itemTypeId, itemIdAndListPosition.first, itemIdAndListPosition.second);
    }

    public static class MatrixAdaptableContextMenuInfo implements ContextMenuInfo {

        public final int itemTypeId;
        public final Integer itemId;
        public final Integer positionInList;

        public MatrixAdaptableContextMenuInfo(int itemTypeId, Integer itemId, Integer positionInList) {
            this.itemTypeId = itemTypeId;
            this.itemId = itemId;
            this.positionInList = positionInList;
        }

    }
}

() Fragment:

public class EntityTableFragment extends Fragment implements TelldusEntityChangeListener {

    protected static final String ARG_TITLE = "title";

    protected MatrixAdapter mAdapter;
    protected ProgressViewer mProgressViewer;
    protected MatrixFilter mFilter;
    protected Handler mHandler = new Handler();
    protected RemoteStickData db;

    public boolean onAttach = false;
    public boolean onCreateView = false;

    private ListView listView;

    public static EntityTableFragment newInstance(String title) {
        EntityTableFragment f = new EntityTableFragment();

        Bundle args = new Bundle();
        args.putString(ARG_TITLE, title);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onAttach(SupportActivity activity) {
        super.onAttach(activity);
        onAttach = true;
            try {
            mProgressViewer = (ProgressViewer) activity;
            mAdapter = new MatrixAdapter(getActivity(), mProgressViewer, new ArrayList<List<MatrixEntity>>(), null);
            TelldusLiveService.addListener(this);
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement ProgressViewer");
        }
        db = new RemoteStickData(getActivity());
    }

    @Override
    public void onDetach() {
        super.onDetach();
        TelldusLiveService.removeListener(this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView.setAdapter(mAdapter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list, null);
        listView = (ListView) view.findViewById(R.id.list);
        return view;
    }

    @Override
    public String toString() {
        return getArguments().getString(ARG_TITLE);
    }

    public Pair<Integer, Integer> getIdAndPositionForView(View v, boolean isLeft) {
        if(listView == null)
            throw new IllegalStateException("Listview not yet created");
        int positionForView = listView.getPositionForView(v);
        if(isLeft)
            return new Pair<Integer, Integer>(mAdapter.getItem(positionForView).get(0).getId(), positionForView);
        else
            return new Pair<Integer, Integer>(mAdapter.getItem(positionForView).get(1).getId(), positionForView);
    }

, ( ), ViewPager ListView . IllegalStateException, , , ?

/ , Log.d(Constants.TAG, "onCreateView!!!!"); onCreateView , ( ViewPager), , !? ?

+5
1

, -, FragmentPagerAdapter ( ). , getItem() . . , FragmentPagerAdapter FragmentManager.

, , findFragmentByTag() ( FragmentManager), makeFragmentName(), . FragmentPagerAdapter . , , .

+4

All Articles