在ViewPager中未使用Fragment调用onCreateView

问题描述 投票:5回答:2

我正在使用来自ACL的ViewPager来展示几个Fragments。首先,这些片段所有ListFragments都包含一个列表。我正在尝试模仿2列表,因此列表中的每个元素都包含另外两个对象的列表。我希望每个单元格都可以长按(不是整个列表项),因此我实现了自己的ColumnLayout(也许它应该被称为CellLayout)。每个列表项包含两个可长按的ColumnLayouts,并实现getContextMenuInfo()以返回有关长按哪个实体的信息。此方法查找ViewPager,请求当前片段,然后调用片段以从长时间单击的视图中返回实体ID。为了获得片段需要执行的正确行:

getListView().getPositionForView(v)

这就是问题开始的地方。有时getListView()抛出IllegalStateException说“内容视图尚未创建”。即onCreateView(...)没有被召集。这有时仅在应用程序恢复时发生。今天,我设法通过在我的Galaxy Nexus上的设置>开发人员选项中启用“不要保持活动”选项来重新产生问题。我启动应用程序,按Home,然后从最近的应用程序菜单重新打开应用程序,现在,如果我长按listview中的任何单元格,则抛出此异常。通过一些调试输出,我设法验证从未调用onCreateView。这有点奇怪,因为整个ViewPager及其Fragment及其ListView及其单元格项目都被绘制出来了!

我被告知在#android-dev上ACL中的ListFragment不支持自定义布局所以我重新实现了我的android.support.v4.app.Fragment而没有使用ListFragment,但这没有帮助。

简短摘要:我有一个自定义布局,处理长按布局的事件,目的是创建一个MenuInfo对象,其中包含有关按下的单元格中的权限的信息。为了找到布局中包含的实体,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及其Fragments及其ListView及其自定义布局都被绘制出来。然而,如果我长按listview中的任何一个单元格,我得到IllegalStateException说这个视图还没有创建?

编辑/实际上,Log.d(Constants.TAG, "onCreateView!!!!");中的onCreateView输出显示方法在我第一次启动应用程序时被调用两次(ViewPager中每个Fragment一次)但是当应用程序恢复时它永远不再被调用!这是一个错误还是我可能做错了什么?

android fragment illegalstateexception android-viewpager
2个回答
4
投票

重要的部分似乎是FragmentPagerAdapter(我没有包括)。调试显示重新创建活动时不调用getItem()。碎片是通过其他方式重新创建的。这意味着我对FragmentPagerAdapter的实现错误地引用了FragmentManager中的碎片。

因此,当我需要查找片段时,我使用findFragmentByTag()(在FragmentManager上)使用makeFragmentName()来获取相应的标签名称。这是添加片段时FragmentPagerAdapter使用的方法。但不确定这是否是一种安全的方法。


0
投票

最好使用FragmentStatePagerAdapter,以便相应地重新创建片段。

© www.soinside.com 2019 - 2024. All rights reserved.