TextView扩展; UnsupportedOperationException异常

时间:2012-10-31 00:12:55

标签: java android android-layout android-view

我已将TextView扩展为同时实施Checkable。除了TextView之外,我没有触及任何onCreateDrawableState内容。此外,我刚刚实现了Checkable方法。 (为清楚起见,删节。)

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;
}

它在ICS上编译得很好,但在Android 2.3中,当程序试图给这个视图充气时,我得到一个非常奇怪的错误。

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

我似乎无法找出这个类型= 0x2的含义。

这是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>

1 个答案:

答案 0 :(得分:1)

?android:attr/listPreferredItemPaddingLeft?android:attr/listPreferredItemPaddingRight仅适用于API 14+(ICS及更高版本)。因此,它并未将其解释为API 9-10(2.3)中的维度。

我建议使用你自己的价值观。如果您熟悉资源文件,请添加<dimen>属性,其填充值与您在ICS上看到的填充值类似,然后引用该属性。

如果您不熟悉自定义XML值,请查看at the dimen documentation