1

I'm trying to inflate an XML, as an item for RecyclerView, the XML is Constraint Layout with two TextViews both TextViews have the same background that I created, it's located in the drawable folder and it's a rectangle shape, in order to create a frame line around the textviews.

But when I run the app, I get this error:

Binary XML file line #9: Error inflating class TextView

From reading similar questions, I found out it might happen because of some properties of the textview, and I found in my case when I remove those backgrounds from the textviews, the inflation works, and everything is fine.

Can anyone explain why this happens? The XML, the drawable shape, and the error are below:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >

    <TextView
        android:id="@+id/country_name"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:textSize="15sp"
        android:text="@string/countryname"
        android:padding="4dp"
        app:layout_constraintHorizontal_weight="50"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/native_name"
        android:gravity="center_horizontal"
        android:background="@drawable/border"
        />

    <TextView
        android:id="@+id/native_name"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:textSize="15sp"
        android:text="@string/nativename"
        android:padding="4dp"
        app:layout_constraintHorizontal_weight="50"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/country_name"
        android:gravity="center_horizontal"
        android:background="@drawable/border"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="@android:color/transparent" />
    <stroke android:width="1dp" android:color="#128FD5"/>
</shape>

Stack trace:

Process: com.example.mydemoproj, PID: 7844
    android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class TextView
        at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at com.example.mydemoproj.CountriesRecyclerView.RecyclerViewAdaper.onCreateViewHolder(RecyclerViewAdaper.kt:18)
        at com.example.mydemoproj.CountriesRecyclerView.RecyclerViewAdaper.onCreateViewHolder(RecyclerViewAdaper.kt:12)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:16630)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
        at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1732)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1497)
        at android.view.View.layout(View.java:16630)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
        at android.view.View.layout(View.java:16630)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
        at android.view.View.layout(View.java:16630)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:530)
        at android.view.View.layout(View.java:16630)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
        at android.view.View.layout(View.java:16630)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
        at android.view.View.layout(View.java:16630)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
        at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
        at android.view.View.layout(View.java:16630)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
        at android.view.Choreographer.doCallbacks(Choreographer.java:670)
        at android.view.Choreographer.doFrame(Choreographer.java:606)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
        at android.os.Handler.
Mark Adler
  • 101,978
  • 13
  • 118
  • 158
Akiva
  • 3
  • 2
  • override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CountryViewHolder { // val inflatedView = parent.inflate(R.layout.main_recycler_country, false) val layoutInflater = LayoutInflater.from(parent.context) val inflatedItem = layoutInflater.inflate(R.layout.main_country_item, parent, false) // var inflatedView = LayoutInflater.from(parent.context).inflate(R.layout.main_country_item, parent, false) return CountryViewHolder(inflatedItem) } *** this is my inflate code from Recycler – Akiva Feb 25 '21 at 07:58
  • don't add additional info in as comments, [edit](https://stackoverflow.com/posts/66364445/edit) your question to include this info. if you need help, you're welcome to @ me – a_local_nobody Feb 25 '21 at 08:01
  • 1
    make sure that your text view background is drawable, not v24/drawable – Muhammad Asad Feb 25 '21 at 08:06
  • @MuhammadAsad, you are right, it was v24 , can you please explain what's the difference, or why v24 doesn't work and normal drawable folder works? – Akiva Feb 25 '21 at 08:15
  • v24 is specifically for devices making use of android api 24 and up, drawable is for all devices – a_local_nobody Feb 25 '21 at 08:21
  • @a_local_nobody, indeed my emulator is API 23, so it would be correct always to put your drawables in the normal folder, and only if you have other version with any v24 features, put also in v24? – Akiva Feb 25 '21 at 08:39
  • any v24 specific stuff goes to v24 yes – a_local_nobody Feb 25 '21 at 08:40

0 Answers0