pak*_*ldt 5 android fragment illegalstateexception android-viewpager
我使用ViewPagerACL来显示几个Fragments.首先,这些片段中的所有片段都ListFragment包含列表.我正在尝试模仿2列表,因此列表中的每个元素都包含另外两个对象的列表.我希望每个单元格都可以长按(不是整个列表项),因此我实现了自己的ColumnLayout(也许它应该被称为CellLayout).每个列表项包含两个可长按的ColumnLayouts,并实现getContextMenuInfo()返回有关长按哪个实体的信息.此方法查找ViewPager,请求当前片段,然后调用片段以从长时间单击的视图中返回实体ID.为了获得片段需要执行的正确行:
getListView().getPositionForView(v)
Run Code Online (Sandbox Code Playgroud)
这就是问题开始的地方.有时会getListView()抛出IllegalStateException"内容视图尚未创建".即onCreateView(...)没有被召唤.这有时仅在应用程序恢复时发生.今天,我设法通过在我的Galaxy Nexus上的设置>开发人员选项中启用"不要保持活动"选项来重新产生问题.我启动应用程序,按Home,然后从最近的应用程序菜单重新打开应用程序,现在,如果我长按listview中的任何单元格,则抛出此异常.通过一些调试输出,我设法验证从未调用onCreateView.这有点奇怪,因为整个ViewPager及其Fragment及其ListView及其单元格项目都被绘制出来了!
我在#android-dev上被告知ListFragment在ACL中不支持自定义布局,所以我重新实现了我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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是(一个片段)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);
}
Run Code Online (Sandbox Code Playgroud)
恢复应用程序(如果活动被销毁)并不奇怪,ViewPager及其Fragments及其ListView及其自定义布局都被绘制出来.然而,IllegalStateException如果我长按listview中的任何单元格,我会说这个视图还没有创建?
编辑/实际上,Log.d(Constants.TAG, "onCreateView!!!!");输出onCreateView显示方法在我第一次启动应用程序时被调用两次(一个用于ViewPager中的每个片段)但是当应用程序恢复时它永远不再被调用!这是一个错误还是我可能做错了什么?
重要的部分似乎是FragmentPagerAdapter(我没有包括在内)。调试显示getItem()重新创建活动时未调用。这些片段是通过其他方式重新创建的。这意味着我实现FragmentPagerAdapter在片段有错误的引用FragmentManager。
因此,相反,当我需要查找片段时,我使用findFragmentByTag()(在 上FragmentManager)使用makeFragmentName()来获取适当的标记名称。这FragmentPagerAdapter是添加片段时使用的方法。不确定这是否是一种安全的方法。
| 归档时间: |
|
| 查看次数: |
8942 次 |
| 最近记录: |