0

I'm having the following error while inflating a custom view component:

android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class .ScrollableRecyclerView

In line 68 inside TabFragment.java

View view = inflater.inflate(R.layout.tab_fragment, container, false);

Full log:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: br.com.osupernerd.osupernerd, PID: 1048
                  android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class br.com.osupernerd.osn.components.ScrollableRecyclerView
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
                      at br.com.osupernerd.osn.fragments.TabFragment.onCreateView(TabFragment.java:68)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                      at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643)
                      at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
                      at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1272)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
                      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
                      at android.view.View.measure(View.java:20214)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                      at android.view.View.measure(View.java:20214)
                      at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
                      at android.view.View.measure(View.java:20214)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                      at android.view.View.measure(View.java:20214)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                      at android.view.View.measure(View.java:20214)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                      at android.view.View.measure(View.java:20214)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                      at android.view.View.measure(View.java:20214)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6333)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                      at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3083)
                      at android.view.View.measure(View.java:20214)
                      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2683)
                      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1636)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1928)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1524)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7520)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                      at android.view.Choreographer.doFrame(Choreographer.java:622)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:7325)
                      at java.lang.reflect.Method.invoke(Native Method)

ScrollableRecyclerView The class below is a custom RecyclerView, in the future it will implement some resources to control scroll parameters. For test purposes the init() method prints a log when it's ready.

This class will implement a custom view to "Pull to Refresh" and controller it inside RecyclerView.

public class ScrollableRecyclerView extends RecyclerView
{
    private static final String TAG = "ScrollableRecyclerView";

    private LayoutInflater inflater;

    private RelativeLayout refreshView;
    private TextView refreshViewText;
    private ImageView refreshViewIcon;
    private ProgressBar refreshViewProgress;

    public ScrollableRecyclerView ( Context context )
    { super( context ); init( context ); }

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

    public ScrollableRecyclerView ( Context context, AttributeSet attrs, int defStyle )
    { super( context, attrs, defStyle ); init( context ); }

    public void init ( Context context )
    {
        inflater = (LayoutInflater) context.getSystemService ( Context.LAYOUT_INFLATER_SERVICE );
        refreshView = (RelativeLayout) inflater.inflate ( R.layout.refresh_header, this, false );

        refreshViewText = (TextView) refreshView.findViewById ( R.id.refresh_text );
        refreshViewIcon = (ImageView) refreshView.findViewById ( R.id.refresh_icon );
        refreshViewProgress = (ProgressBar) refreshView.findViewById ( R.id.refresh_progress );

        addView ( refreshView );

        Log.d( TAG, "Ready...");
    }

}

TabFragment Here, it's the Fragment that will have ScrollableRecyclerView custom component. It's important to say that nothing wrong is happening between MainActivity and TabFragment.

public class TabFragment extends Fragment implements IHTTPCallback
{
    public final String TAG = "osn.TabFragment";

    private ScrollableRecyclerView recyclerView;

    public TabFragment ()
    {}

    @Override
    public void onCreate( Bundle savedInstanceState )
    { super.onCreate(savedInstanceState); }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.tab_fragment, container, false);

        this.recyclerView = (ScrollableRecyclerView) view.findViewById(R.id.posts_view);
        this.recyclerView.setLayoutManager( new LinearLayoutManager( getContext() ) );

        return view;
}

Here is tab_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackground"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

    <br.com.osupernerd.osn.components.ScrollableRecyclerView
        android:id="@+id/posts_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:scrollbars="vertical">
    </br.com.osupernerd.osn.components.ScrollableRecyclerView>

</RelativeLayout>

Here is refresh_header.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/refresh_header"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="10dp"
    android:paddingBottom="15dp"
    android:gravity="center"
    >    
    <ProgressBar
        android:id="@+id/refresh_progress"
        android:indeterminate="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="10dip"
        android:visibility="gone"
        android:layout_centerVertical="true"
        style="?android:attr/progressBarStyleSmall"
        />
    <ImageView
        android:id="@+id/refresh_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dip"
        android:layout_marginRight="20dip"
        android:visibility="gone"
        android:layout_gravity="center"
        android:gravity="center"
        android:src="@drawable/ic_down"
        />
    <TextView
        android:id="@+id/refresh_text"
        android:text="@string/refresh_tap_label"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold"
        android:paddingTop="5dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        />    
</RelativeLayout>

OBSERVATIONS

When I use RecyclerView instead ScrollableRecyclerView... all works well!

When I remove the following code below, from ScrollableRecyclerView... all works well!

refreshView = (RelativeLayout) inflater.inflate ( R.layout.refresh_header, this, false );

refreshViewText = (TextView) refreshView.findViewById ( R.id.refresh_text );
refreshViewIcon = (ImageView) refreshView.findViewById ( R.id.refresh_icon );
refreshViewProgress = (ProgressBar) refreshView.findViewById ( R.id.refresh_progress );

addView ( refreshView );

Someone can help and say what's going wrong? If I can't inflate the component ScrollableRecyclerView, how can I dynamically insert a view inside its?

Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
caiquearaujo
  • 359
  • 4
  • 11
  • Recyclerview is already scrollable. What's this class do? – OneCricketeer Dec 06 '16 at 15:09
  • Anyways, show the full logcat please – OneCricketeer Dec 06 '16 at 15:10
  • @cricket_007 This class will implement a custom view to "Pull to Refresh" and controller it inside RecyclerView, also will control "load more" function at some scroll point. – caiquearaujo Dec 06 '16 at 15:19
  • @cricket_007 and I updated with the full logcat! – caiquearaujo Dec 06 '16 at 15:20
  • So, you're saying the error only occurs when you inflate a view within your other view? Not too sure - haven't used custom views too much. I was expected the logcat to be more helpful – OneCricketeer Dec 06 '16 at 15:23
  • 1
    do you have any `Caused by: ` on the logcat? – pskink Dec 06 '16 at 15:24
  • @cricket_007 yes, the error only occurs when I inflate inside ScrollableRecyclerView class, I don't know if it's a problem with RecyclerView maybe... – caiquearaujo Dec 06 '16 at 15:27
  • @pskink there's no `Caused by:` on the logcat – caiquearaujo Dec 06 '16 at 15:28
  • so just for testing place your `inflater.inflate` in `try / catch InflateException` and call `e.printStackTrace` or `Log.d` with `e.getCause()` – pskink Dec 06 '16 at 15:31
  • I do think it's strange you inflate a layout with `layout_height="fill_parent"` into a vertical list. That's probably not the problem, though – OneCricketeer Dec 06 '16 at 15:31
  • @cricket_007 it's the initiable form, I will control this layout height in ScrollableRecyclerView. It will begin as fill_parent while there is no data. – caiquearaujo Dec 06 '16 at 15:40
  • @pskink I got `Caused by: java.lang.IllegalStateException: RecyclerView has no LayoutManager`. It's strange because I've setted LayoutManager to it in the TabFramework. – caiquearaujo Dec 06 '16 at 15:43
  • so add a breakpoint in `ScrollableRecyclerView#init` and go step-by-step – pskink Dec 06 '16 at 15:46
  • but i think your exception comes from `addView ( refreshView );` – pskink Dec 06 '16 at 15:52
  • @pskink it goes to InflateException after `refreshView = ( RelativeLayout ) inflater.inflate ( R.layout.refresh_header, this, false );` line... Then I did the following: I added `if ( this.getLayoutManager () == null ) { this.setLayoutManager ( new LinearLayoutManager ( context ) ); }` inside `ScrollableRecyclerView#init` and I removed `this.recyclerView.setLayoutManager( new LinearLayoutManager( getContext() ) );` from `TabFragment#onCreateView`. Now there is no error... but the recyclerView isn't added into ScrollableRecyclerView. – caiquearaujo Dec 06 '16 at 15:56
  • @pskink I also think the problem is `addView ( refreshView );` because RecyclerView needs a ViewHolder, but I don't know how to insert refreshView inside ScrollableRecyclerView – caiquearaujo Dec 06 '16 at 15:58
  • If you are trying to add a header view, this post looks good. http://stackoverflow.com/questions/26530685/is-there-an-addheaderview-equivalent-for-recyclerview – OneCricketeer Dec 06 '16 at 16:05
  • @cricket_007 thank you! – caiquearaujo Dec 06 '16 at 16:22

0 Answers0