Home > database >  Why got fatal exception: Error inflating class Spinner
Why got fatal exception: Error inflating class Spinner

Time:11-21

I would like create my first Spinner.

Work fine, but if I try put the "expand more" arrow, throw an exception.

If I remove item/Bitmap part from gradient_spinner.xml then work fine without error.

layout/activity_settings.xml:

       <LinearLayout
            style="?metaButtonBarStyle"
            android:id="@ id/activity_settings_ll03_hori"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:paddingTop="10dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/button_draw_head_yellow"
            >
            <TextView
                android:id="@ id/storesSettingsTextview"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/store_settings"
                android:textColor="@color/dark_grey"
                android:textSize="20sp"
                android:textStyle="bold" />

            <Spinner
                android:id="@ id/storeList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                style="@style/Widget.App.Spinner"

                />

        </LinearLayout>

style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="@style/Theme.AppCompat">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="spinnerStyle">@style/Widget.App.Spinner</item>
        <item name="android:spinnerStyle">@style/Widget.App.Spinner</item>

    </style>
    <style name="CheckBoxGreyGreen" parent="@style/Theme.AppCompat.Light">
        <item name="colorControlNormal">@color/dark_grey</item>
        <item name="colorControlActivated">@color/box_green</item>
    </style>
    <style name="RadioButtonGreyGreen" parent="@style/Theme.AppCompat.Light">
        <item name="colorControlNormal">@color/dark_grey</item>
        <item name="colorControlActivated">@color/box_green</item>
    </style>

    <style name="Widget.App.Spinner" parent="@style/Widget.AppCompat.Spinner">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:background">@drawable/gradient_spinner</item>
        <item name="android:layout_margin">10dp</item>
        <item name="android:paddingLeft">2dp</item>
        <item name="android:paddingRight">2dp</item>
        <item name="android:paddingTop">1dp</item>
        <item name="android:paddingBottom">1dp</item>
        <item name="android:popupBackground">@color/light_grey_bg</item>
    </style>
</resources>

drawable/gradient_spinner.xml (not gradient)

<item>
    <layer-list>
        <item>
            <shape android:shape="rectangle">
                <stroke android:width="1dp" android:color="#727c82" />
                <corners android:radius="5dp" />
                <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
            </shape>
        </item>
        <item>
            <bitmap
            android:src="@drawable/ic_expand_more_black_24dp"
            android:tint="@color/colorPrimary"
            android:gravity="bottom|right"/>
        </item>>
   </layer-list>
</item>

Dropdown content design activity_store_list.xml

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@ id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="18dip"
    android:paddingTop="8dip"
    android:paddingRight="8dip"
    android:paddingBottom="8dip"
    android:textSize="16sp"
    android:textColor="@color/dark_grey"
    android:textStyle="bold" >
</TextView>

The arrow drawable/ic_expand_more_black_24dp.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
  <path
      android:pathData="M16.59,8.59L12,13.17 7.41,8.59 6,10l6,6 6,-6z"
      android:fillColor="#000000"/>
</vector>

AppCompActivity part, where config, the spinner

 val adapter: ArrayAdapter<*> = ArrayAdapter<String>(this, R.layout.activity_store_list, storeList )
        val listSpinner= findViewById<View>(R.id.storeList) as Spinner
        listSpinner.onItemSelectedListener
        listSpinner.adapter = adapter

        listSpinner.setSelection(ItemPosition)
        listSpinner.onItemSelectedListener = this

I read many other stack owerflow thread and try that, but not work for me. For example: Remove bitmap and use item for arrow: This throw exception too: Error inflating class Spinner

<item>
    <layer-list>
        <item>
            <shape android:shape="rectangle">
                <stroke android:width="1dp" android:color="#727c82" />
                <corners android:radius="5dp" />
                <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
            </shape>
        </item>
        <item
            
            android:src="@drawable/ic_expand_more_black_24dp"
            android:tint="@color/colorPrimary"
            android:gravity="bottom|right">
        </item>
   </layer-list>
</item>

Log:

    2021-11-19 16:29:49.249 22334-22334/hu.fherp.application D/AndroidRuntime: Shutting down VM
    
    
    --------- beginning of crash
2021-11-19 16:29:49.254 22334-22334/hu.fherp.application E/AndroidRuntime: FATAL EXCEPTION: main
    Process: hu.fherp.application, PID: 22334
    java.lang.RuntimeException: Unable to start activity ComponentInfo{hu.fherp.application/hu.fherp.application.SettingsActivity}: android.view.InflateException: Binary XML file line #170 in hu.fherp.application:layout/activity_settings: Binary XML file line #170 in hu.fherp.application:layout/activity_settings: Error inflating class Spinner
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3792)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3968)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8512)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1139)
     Caused by: android.view.InflateException: Binary XML file line #170 in hu.fherp.application:layout/activity_settings: Binary XML file line #170 in hu.fherp.application:layout/activity_settings: Error inflating class Spinner
     Caused by: android.view.InflateException: Binary XML file line #170 in hu.fherp.application:layout/activity_settings: Error inflating class Spinner
     Caused by: android.content.res.Resources$NotFoundException: Drawable hu.fherp.application:drawable/gradient_spinner with resource ID #0x7f07006a
     Caused by: android.content.res.Resources$NotFoundException: File res/drawable/gradient_spinner.xml from drawable resource ID #0x7f07006a
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:1104)
        at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:795)
        at android.content.res.Resources.loadDrawable(Resources.java:1098)
        at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:1006)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:981)
        at android.view.View.<init>(View.java:5954)
        at android.view.ViewGroup.<init>(ViewGroup.java:715)
        at android.widget.AdapterView.<init>(AdapterView.java:305)
        at android.widget.AbsSpinner.<init>(AbsSpinner.java:74)
        at android.widget.Spinner.<init>(Spinner.java:258)
        at android.widget.Spinner.<init>(Spinner.java:225)
        at android.widget.Spinner.<init>(Spinner.java:175)
        at androidx.appcompat.widget.AppCompatSpinner.<init>(AppCompatSpinner.java:209)
        at androidx.appcompat.widget.AppCompatSpinner.<init>(AppCompatSpinner.java:180)
        at androidx.appcompat.widget.AppCompatSpinner.<init>(AppCompatSpinner.java:160)
        at androidx.appcompat.widget.AppCompatSpinner.<init>(AppCompatSpinner.java:144)
        at androidx.appcompat.app.AppCompatViewInflater.createSpinner(AppCompatViewInflater.java:214)
        at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:131)
        at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1563)
        at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1614)
        at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1059)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
2021-11-19 16:29:49.259 22334-22334/hu.fherp.application E/AndroidRuntime:     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
        at hu.fherp.application.SettingsActivity.onCreate(SettingsActivity.kt:21)
        at android.app.Activity.performCreate(Activity.java:8198)
        at android.app.Activity.performCreate(Activity.java:8182)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3765)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3968)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8512)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1139)
     Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: <bitmap> requires a valid 'src' attribute
        at android.graphics.drawable.BitmapDrawable.updateStateFromTypedArray(BitmapDrawable.java:854)
        at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:775)
        at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:148)
        at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1429)
        at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1418)
        at android.graphics.drawable.LayerDrawable.inflateLayers(LayerDrawable.java:284)
        at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:199)
        at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:148)
        at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1429)
        at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1418)
        at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:199)
        at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:126)
        at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:148)
        at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1429)
        at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1388)
        at android.content.res.ResourcesImpl.loadXmlDrawable(ResourcesImpl.java:1167)
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:1064)
            ... 53 more

Any idea?

CodePudding user response:

Caused by: android.view.InflateException: Binary XML file line #170 in
hu.fherp.application:layout/activity_settings: Binary XML file line #170 in
hu.fherp.application:layout/activity_settings: Error inflating class Spinner
...
Caused by: android.content.res.Resources$NotFoundException:
File res/drawable/gradient_spinner.xml from drawable resource ID #0x7f07006a
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17:
<bitmap> requires a valid 'src' attribute

I'm guessing these are two separate errors.

A <bitmap> resource has to point to a bitmap image, i.e. a .jpg, .gif or .png. You can't use a vector drawable as its src, those aren't bitmaps.

If you want to use a vector drawable instead, you need to use android:drawable instead of android:src. Also your layer-list needs to be the parent element of the drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#727c82" />
            <corners android:radius="5dp" />
            <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
        </shape>
    </item>
    <item
        android:drawable="@drawable/ic_expand_more_black_24dp"
        android:tint="@color/colorPrimary"
        android:gravity="bottom|right">
    </item>
</layer-list>

I don't know if that'll fix the not found crash for gradient_spinner.xml, the code you've posted shouldn't compile so it's hard to know, but that should give you some stuff to look at at least

  • Related