13

I added an attribute to the android attrs.xml file for different color shades. In the styles.xml file I give these attributes colors, so they are different for each style.

Attrs.xml:

<attr name="lightBackground" format="color" />
<attr name="normalBackground" format="color" />

styles.xml:

<style name="AppThemeNight" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimaryNight</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDarkNight</item>
    <item name="colorAccent">@color/colorAccentNight</item>

    <item name="drawerArrowStyle">@style/DrawerIcon</item>
    <item name="lightBackground">@color/colorPrimaryLightNight</item>
    <item name="normalBackground">@color/colorPrimaryNight</item>
</style>

Inflating a custom spinner layout: spinner_layout_adapter.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/spinnerLayoutAdapter_tv_spinnerText"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:padding="15dp"
      android:text="TextView"
      android:textColor="@android:color/white"
      android:background="?attr/normalBackground"
      android:textSize="16sp"/>

However,

05-23 14:22:34.833 17231-17231/nl.gemeente.breda.bredaapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: nl.gemeente.breda.bredaapp, PID: 17231
    android.view.InflateException: Binary XML file line #3: Binary XML file line #3: Error inflating class android.widget.TextView
    Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class android.widget.TextView
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
        at android.view.LayoutInflater.createView(LayoutInflater.java:645)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
        at nl.gemeente.breda.bredaapp.adapter.ServiceAdapter.getView(ServiceAdapter.java:32)
        at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:194)
        at android.widget.Spinner.onMeasure(Spinner.java:592)
        at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:416)
        at android.view.View.measure(View.java:19861)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:19861)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1117)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:642)
        at android.view.View.measure(View.java:19861)
        at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
        at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
        at android.view.View.measure(View.java:19861)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:19861)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
        at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:786)
        at android.view.View.measure(View.java:19861)
        at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
        at android.view.View.measure(View.java:19861)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
        at android.view.View.measure(View.java:19861)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
        at android.view.View.measure(View.java:19861)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:19861)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
        at android.view.View.measure(View.java:19861)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
05-23 14:22:34.833 17231-17231/nl.gemeente.breda.bredaapp E/AndroidRuntime:     at com.android.internal.policy.DecorView.onMeasure(DecorView.java:691)
        at android.view.View.measure(View.java:19861)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6344)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
        at android.view.Choreographer.doCallbacks(Choreographer.java:686)
        at android.view.Choreographer.doFrame(Choreographer.java:621)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
     Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x7f010046 a=-1}
        at android.content.res.TypedArray.getDrawable(TypedArray.java:925)
        at android.view.View.<init>(View.java:4202)
        at android.widget.TextView.<init>(TextView.java:710)
        at android.widget.TextView.<init>(TextView.java:704)
        at android.widget.TextView.<init>(TextView.java:700)
            ... 71 more

When I change the attribute with a normal color like below, it works. The attribute ?attr/normalBackground doesn't seem to work.

android:background="@color/colorPrimary"

Anyone has an idea what I did wrong?

5
  • Which version of Android were you running? I had something similar with Android O (beta) but only occurred rarely. Commented Jun 29, 2017 at 7:29
  • I have a similar problem with SDK 25. Commented Nov 28, 2017 at 8:32
  • The issue is non-reproducable. Commented Nov 28, 2017 at 13:46
  • My problem was that I didn't use an AppCompat theme for my application. Since Thimo does so as shown in the code above the problem here seems to be something else. Commented Nov 28, 2017 at 15:27
  • 1
    This happens to me when I inflate a layout using android:background="?selectableItemBackground" Commented May 27, 2021 at 14:07

2 Answers 2

15
+50

Where do you define that your "AppThemeNight" is to be used? I set up a small project with your code and defined that "AppThemeNight" is to be used in the manifest and everything worked as expected. When I removed the reference to "AppThemeNight" in the manifest the app errored out with the following error:

Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x7f0200c9 a=-1}

which is what I think that you are seeing, so I don't think that you are specifying that your theme should be used. If you are, please post that, and we'll make another attempt to resolve the issue. For reference, here is my test manifest that works:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.attributes">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppThemeNight">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Sign up to request clarification or add additional context in comments.

2 Comments

Also to get more specific info in catch block log the exception.getMessage()
Yes I've specified my Theme in Menifest but still getting the same issue. Is there anything else? Please help :(
0

In my case I had used android:background="?attr/backgroundColor" in one of the CardViews, turned out it was not recognised but the error was quite ambiguous and did not point out exactly.

In your case, I suspect android:background="?attr/normalBackground" property inside spinner_layout_adapter.xml.

For anyone else, carefully inspect the said file/line in the stack trace and eliminate any stylings, especially ones you know you did not define.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.