我正在尝试将我的数据充气并绑定到我的ViewHolder。每当我绑定时,我都会遇到此错误:
java.lang.IllegalStateException: Layout state should be one of 100 but it is 10
at android.support.v7.widget.RecyclerView$State.assertLayoutStep(RecyclerView.java:10876)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3391)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3194)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3627)
at android.view.View.layout(View.java:13916)
at android.view.ViewGroup.layout(ViewGroup.java:4424)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
at android.view.View.layout(View.java:13916)
at android.view.ViewGroup.layout(ViewGroup.java:4424)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1799)
at android.view.View.layout(View.java:13916)
at android.view.ViewGroup.layout(ViewGroup.java:4424)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
at android.view.View.layout(View.java:13916)
at android.view.ViewGroup.layout(ViewGroup.java:4424)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1367)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:849)
at android.view.View.layout(View.java:13916)
at android.view.ViewGroup.layout(ViewGroup.java:4424)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13916)
at android.view.ViewGroup.layout(ViewGroup.java:4424)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
at android.view.View.layout(View.java:13916)
at android.view.ViewGroup.layout(ViewGroup.java:4424)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13916)
at android.view.ViewGroup.layout(ViewGroup.java:4424)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
at android.view.View.layout(View.java:13916)
at android.view.ViewGroup.layout(ViewGroup.java:4424)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13916)
at android.view.ViewGroup.layout(ViewGroup.java:4424)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2029)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1850)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1143)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4667)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
之前我从未遇到过此错误。关于这一点也没有太多信息。出现这种情况的原因是什么?
修改
我认为问题出现在这段代码上:
public class ProfileAdapter extends RecyclerView.Adapter<TypedHolder>
{
public static final int TYPE_HEADER = 0;
public static final int TYPE_MAP = 1;
private List<GeoMap> mGeoMaps = new ArrayList<>();
private Context mContext;
private User mCurrentUser;
private ProfileHeaderHolderInterface mProfileHeaderHolder;
private DownloadedMapHolderInterface mDownloadedMapHolder;
private ProfileHeaderHolderInterface.Listener mProfileHeaderHolderInterfaceListener;
private DownloadedMapHolderInterface.Listener mDownloadedMapHolderInterfaceListener;
private ProfileHeaderHolderInterface.OnBind mProfileHeaderHolderInterfaceOnBind;
private DownloadedMapHolderInterface.OnBind mDownloadedMapHolderInterfaceOnBind;
public ProfileAdapter(Context context)
{
mContext = context;
mProfileHeaderHolder = new ProfileHeaderHolder(mContext);
mDownloadedMapHolder = new DownloadedMapHolder(mContext);
}
public void setContent(List<GeoMap> map, User user)
{
if(map == null)
return;
mGeoMaps.clear();
mGeoMaps.addAll(map);
mCurrentUser = user;
}
public ProfileHeaderHolderInterface getProfileHeaderView()
{
return mProfileHeaderHolder;
}
@Override
public TypedHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater inflater = LayoutInflater.from(mContext);
switch(viewType)
{
case TYPE_HEADER:
return mProfileHeaderHolder.createHolder(parent);
case TYPE_MAP:
{
View view = inflater.inflate(R.layout.listitem_offline_map, parent, false);
DownloadedMapHolder.ViewHolder holder = new DownloadedMapHolder.ViewHolder(view);
// return mDownloadedMapHolder.createHolder(parent);
return holder;
}
}
return new TypedHolder(new View(mContext), -1);
}
@Override
public int getItemViewType(int position)
{
if(position == 0)
return TYPE_HEADER;
return TYPE_MAP;
}
@Override
public void onBindViewHolder(TypedHolder holder, int position)
{
switch(holder.getType())
{
case ProfileAdapter.TYPE_HEADER:
if(mProfileHeaderHolderInterfaceListener != null)
mProfileHeaderHolderInterfaceOnBind.onBind(holder, mCurrentUser);
break;
case ProfileAdapter.TYPE_MAP:
{
GeoMap geoMap = mGeoMaps.isEmpty()? null : mGeoMaps.get(position - 1);
//if (mDownloadedMapHolderInterfaceListener != null)
// mDownloadedMapHolderInterfaceOnBind.onBind(holder, geoMap, position - 1);
DownloadedMapHolder.ViewHolder downloadedMapHolder = (DownloadedMapHolder.ViewHolder) holder;
downloadedMapHolder.mTextViewLocation.setText(geoMap.getMapLocation());
downloadedMapHolder.mTextViewLocation.setText(geoMap.getDateString());
}
break;
}
}
@Override
public int getItemCount()
{
return mGeoMaps.isEmpty()? 1 : mGeoMaps.size() + 1;
}
public void setProfileHeaderHolderInterfaceListener(ProfileHeaderHolderInterface.Listener listener)
{
mProfileHeaderHolderInterfaceListener = listener;
mProfileHeaderHolder.setProfileHeaderHolderInterfaceListener(listener);
}
public void setDownloadedMapHolderInterfaceListener(DownloadedMapHolderInterface.Listener listener)
{
mDownloadedMapHolderInterfaceListener = listener;
mDownloadedMapHolder.setDownloadedMapHolderInterfaceListener(listener);
}
public void setProfileHeaderHolderInterfaceOnBind(ProfileHeaderHolderInterface.OnBind onBind)
{
mProfileHeaderHolderInterfaceOnBind = onBind;
}
public void setDownloadedMapHolderInterfaceOnBind(DownloadedMapHolderInterface.OnBind onBind)
{
mDownloadedMapHolderInterfaceOnBind = onBind;
}
}
这个问题非常令人沮丧。在剥夺了我的适配器后,我只能看到它的必需品,我能够发现它正在这条线上:
@Override
public int getItemViewType(int position)
{
if(position == 0)
return TYPE_HEADER;
else if(position > 0 && position < mGeoMaps.size())
return TYPE_MAP;
else
{
Log.i(ShoreLeaveApplication.TAG, "CANNOT BIND on TYPE -1!!!!");
return -1;
}
}
我试图创建一个非常简单的回收站视图,其中有一个添加注释卡始终作为我的列表的顶部和第一个元素(类似于FB)。其余的只是地图选择。这会导致我的onCreateViewHolder收到无效的ID并膨胀错误的布局。到达最后一个项目时会发生这种情况。这是一个蹩脚的代码,我急着写下来,用这个代替它:
@Override
public int getItemViewType(int position)
{
if(position == 0) // first item is always the header
return TYPE_HEADER;
// the rest is a normal item
return TYPE_MAP;
}
它现在有效。
谢谢!
答案 0 :(得分:2)
它似乎与代码无关,请尝试在build.gradle中进行以下更改。
在build.gradle的依赖项部分中查找compile com.android.support:appcompat-v7:XX.x.x
,并尝试降级或升级最后一位数。
例如我的是com.android.support:appcompat-v7:24.2.1
,我曾尝试使用com.android.support:appcompat-v7:24.2.0
和com.android.support:appcompat-v7:24.2.2
虽然它有点受欢迎,但是对于较小的项目值得一试,考虑到事实,如果是代码问题,堆栈跟踪(你粘贴在问题中)会指向你特定的行。