Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout state should be one of 100 but it is 10 [closed]

Tags:

I am trying to inflate and bind my data to my ViewHolder. I am having this error whenever I bind:

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)

I never encountered this error before. There is not much info about this either. Any reason why this is happening?

EDIT

I think the problem was on this code:

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;
    }
}

This problem was very frustrating. After I stripped my Adapter with just bare necessities, I was able to figure out it is coming on this line:

@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;
    }
}

I was trying to create a very simple recycler view where there is an add note card always as the top and first element of my list (Akin to FB). The rest is just map selection. This causes my onCreateViewHolder to receive an invalid id and inflates wrong layout. This happens when it reaches the last item. It was a crappy code I wrote on a rush and replace it with this:

@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;
}

And it works now.

Thanks!