0

I have extended a TextView to also implement Checkable. I have not touched any TextView stuff, except for onCreateDrawableState. Furthermore, I just implemented the Checkable methods. (abridged for clarity.)

public class CheckableTextView extends TextView implements Checkable {

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

@Override
protected int[] onCreateDrawableState(int extraSpace) {
    final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
    if (isChecked()) {
        mergeDrawableStates(drawableState, CheckedStateSet);
    }
    return drawableState;
}

It compiles fine on ICS, but in Android 2.3 I get a pretty weird error when the program tried to inflate this view.

10-31 00:51:20.414: E/AndroidRuntime(601): FATAL EXCEPTION: main
10-31 00:51:20.414: E/AndroidRuntime(601): android.view.InflateException: Binary XML file line #3: Error inflating class net.blackenvelope.utrechtafval.map.layouts.CheckableTextView

...

10-31 00:51:20.414: E/AndroidRuntime(601): Caused by: java.lang.reflect.InvocationTargetException
10-31 00:51:20.414: E/AndroidRuntime(601):  at java.lang.reflect.Constructor.constructNative(Native Method)
10-31 00:51:20.414: E/AndroidRuntime(601):  at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
10-31 00:51:20.414: E/AndroidRuntime(601):  at android.view.LayoutInflater.createView(LayoutInflater.java:505)
10-31 00:51:20.414: E/AndroidRuntime(601):  ... 45 more
10-31 00:51:20.414: E/AndroidRuntime(601): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
10-31 00:51:20.414: E/AndroidRuntime(601):  at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
10-31 00:51:20.414: E/AndroidRuntime(601):  at android.view.View.<init>(View.java:1963)
10-31 00:51:20.414: E/AndroidRuntime(601):  at android.widget.TextView.<init>(TextView.java:344)
10-31 00:51:20.414: E/AndroidRuntime(601):  at android.widget.TextView.<init>(TextView.java:337)
10-31 00:51:20.414: E/AndroidRuntime(601):  at net.app.layouts.CheckableTextView.<init>(CheckableTextView.java:11)
10-31 00:51:20.414: E/AndroidRuntime(601):  ... 48 more

I can't seem to find out what this type=0x2 means.

Here is the XML:

<?xml version="1.0" encoding="utf-8"?>

<app.CheckableTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/overlay_type_title"
    android:layout_width="match_parent"
    android:background="@drawable/overlay_list_bg_selector"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/overlay_list_icon"
    android:drawablePadding="10dp"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:paddingLeft="10dp"
    android:paddingRight="?android:attr/listPreferredItemPaddingRight"
    android:textAppearance="?android:attr/textAppearanceListItemSmall" 
    >
</app.CheckableTextView>
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Maarten
  • 6,894
  • 7
  • 55
  • 90
  • 1
    It means you're passing an attribute as a dimension value, probably in your XML. Can we see the XML file you're inflating from? – Cat Oct 31 '12 at 00:18
  • Ah, I don't really get this attribute business. I think I copied it from some example somewhere. How would I go about hardcoding these attributes? Would that be wise? – Maarten Oct 31 '12 at 00:32

1 Answers1

1

?android:attr/listPreferredItemPaddingLeft and ?android:attr/listPreferredItemPaddingRight are available only in API 14+ (ICS and later). Thus, it is not interpreting it as a dimension on API 9-10 (2.3).

I would suggest using your own values. If you're familiar with resource files, add a <dimen> attribute with a padding value similar to the one you're seeing on ICS, then reference that.

If you're unfamiliar with custom XML values, have a look at the dimen documentation.

Cat
  • 66,919
  • 24
  • 133
  • 141
  • Is there a way to get the ICS values through something else than trial & error? Like finding the source code? – Maarten Oct 31 '12 at 12:58
  • 1
    I know they're in the source somewhere, but not sure where. You can, however, use `activity.getTheme().resolveAttribute(android.R.attr.your_attribute_name_here, value, true);` to get the value. Example of that [in this answer](http://stackoverflow.com/a/5982483/1438733). – Cat Oct 31 '12 at 18:19