java - Android 错误膨胀类 <unknown>

标签 java android xml fonts textview

我正在尝试实现我在 stackoverflow 上找到的 RobotoTextView,代码相当简单。

    public class RobotoTextView extends TextView {

    private static final int TYPEFACE_COUNT = 16;

    private final static int ROBOTO_THIN = 0;
    private final static int ROBOTO_THIN_ITALIC = 1;
    private final static int ROBOTO_LIGHT = 2;
    private final static int ROBOTO_LIGHT_ITALIC = 3;
    private final static int ROBOTO_REGULAR = 4;
    private final static int ROBOTO_ITALIC = 5;
    private final static int ROBOTO_MEDIUM = 6;
    private final static int ROBOTO_MEDIUM_ITALIC = 7;
    private final static int ROBOTO_BOLD = 8;
    private final static int ROBOTO_BOLD_ITALIC = 9;
    private final static int ROBOTO_BLACK = 10;
    private final static int ROBOTO_BLACK_ITALIC = 11;
    private final static int ROBOTO_CONDENSED = 12;
    private final static int ROBOTO_CONDENSED_ITALIC = 13;
    private final static int ROBOTO_CONDENSED_BOLD = 14;
    private final static int ROBOTO_CONDENSED_BOLD_ITALIC = 15;


    private static final SparseArray<Typeface> mTypefaces = new SparseArray<>(TYPEFACE_COUNT);


    public RobotoTextView(Context context) {
        super(context);
    }

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

    public RobotoTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        parseAttributes(context, attrs);
    }

    private void parseAttributes(Context context, AttributeSet attrs) {
        TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.RobotoTextView);

        int typefaceValue = values.getInt(R.styleable.RobotoTextView_typeface, 0);
        setTypeface(obtaintTypeface(context, typefaceValue));
        values.recycle();
    }

    private Typeface obtaintTypeface(Context context, int typefaceValue) throws IllegalArgumentException {
        Typeface typeface = mTypefaces.get(typefaceValue);
        if (typeface == null) {
            typeface = createTypeface(context, typefaceValue);
            mTypefaces.put(typefaceValue, typeface);
        }
        return typeface;
    }

    private Typeface createTypeface(Context context, int typefaceValue) throws IllegalArgumentException {
        Typeface typeface;
        switch (typefaceValue) {
            case ROBOTO_THIN:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Thin.ttf");
                break;
            case ROBOTO_THIN_ITALIC:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-ThinItalic.ttf");
                break;
            case ROBOTO_LIGHT:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf");
                break;
            case ROBOTO_LIGHT_ITALIC:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-LightItalic.ttf");
                break;
            case ROBOTO_REGULAR:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf");
                break;
            case ROBOTO_ITALIC:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Italic.ttf");
                break;
            case ROBOTO_MEDIUM:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf");
                break;
            case ROBOTO_MEDIUM_ITALIC:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-MediumItalic.ttf");
                break;
            case ROBOTO_BOLD:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
                break;
            case ROBOTO_BOLD_ITALIC:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldItalic.ttf");
                break;
            case ROBOTO_BLACK:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf");
                break;
            case ROBOTO_BLACK_ITALIC:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BlackItalic.ttf");
                break;
            case ROBOTO_CONDENSED:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Condensed.ttf");
                break;
            case ROBOTO_CONDENSED_ITALIC:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-CondensedItalic.ttf");
                break;
            case ROBOTO_CONDENSED_BOLD:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldCondensed.ttf");
                break;
            case ROBOTO_CONDENSED_BOLD_ITALIC:
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldCondensedItalic.ttf");
                break;
            default:
                throw new IllegalArgumentException("Unknown `typeface` attribute value " + typefaceValue);
        }
        return typeface;
    }
    }

这是 XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="72dp"
        android:background="?attr/selectableItemBackground">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_centerVertical="true"
            android:layout_marginStart="8dp"
            android:gravity="center"
            android:padding="8dp"
            android:scaleType="centerInside"
            android:src="@drawable/ic_action_star_10"
            android:visibility="invisible" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="72dp"
            android:layout_toStartOf="@+id/overflow"
            android:gravity="center"
            android:orientation="vertical">

            <!--<TextView-->
            <!--android:id="@+id/editTextRoutineName"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:text="Title"-->
            <!--android:textColor="@color/textColorTitleDark"-->
            <!--android:textAppearance="@style/TextAppearance.AppCompat.Title"/>-->

            <!--<TextView-->
            <!--android:id="@+id/subtitle"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:text="Subtitle"-->
            <!--android:maxLines="2"-->
            <!--android:textColor="@color/textColorSubTitleDark"-->
            <!--android:textAppearance="@style/TextAppearance.AppCompat.Subhead"/>-->

            <TextView
                android:id="@+id/subtitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLines="2"
                android:text="Subtitle"
                android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
                android:textColor="@color/textColorSubTitleDark" />

            <com.periapsislabs.trackin.views.RobotoTextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:typeface="roboto_condensed_bold" />


        </LinearLayout>


        <ImageView
            android:id="@+id/overflow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentStart="false"
            android:layout_centerVertical="true"
            android:background="?attr/selectableItemBackground"
            android:padding="16dp"
            android:src="@drawable/ic_action_overflow"
            android:visibility="gone" />

        <View
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="72dp"
            android:background="@drawable/divider"
            android:visibility="gone" />

    </RelativeLayout>


</RelativeLayout>

和堆栈跟踪

    android.view.InflateException: Binary XML file line #59: Error inflating class <unknown>
            at android.view.LayoutInflater.createView(LayoutInflater.java:633)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at com.periapsislabs.trackin.exercise.ExercisesFragment$ExerciseAdapter.onCreateViewHolder(ExercisesFragment.java:227)
            at com.periapsislabs.trackin.exercise.ExercisesFragment$ExerciseAdapter.onCreateViewHolder(ExercisesFragment.java:210)
            at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:4121)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3431)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1306)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:523)
            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1988)
            at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237)
            at android.view.View.layout(View.java:15596)
            at android.view.ViewGroup.layout(ViewGroup.java:4966)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:15596)
            at android.view.ViewGroup.layout(ViewGroup.java:4966)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:15596)
            at android.view.ViewGroup.layout(ViewGroup.java:4966)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
            at android.view.View.layout(View.java:15596)
            at android.view.ViewGroup.layout(ViewGroup.java:4966)
            at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:890)
            at android.view.View.layout(View.java:15596)
            at android.view.ViewGroup.layout(ViewGroup.java:4966)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:15596)
            at android.view.ViewGroup.layout(ViewGroup.java:4966)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15596)
            at android.view.ViewGroup.layout(ViewGroup.java:4966)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:15596)
            at android.view.ViewGroup.layout(ViewGroup.java:4966)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15596)
            at android.view.ViewGroup.layout(ViewGroup.java:4966)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:15596)
            at android.view.ViewGroup.layout(ViewGroup.java:4966)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)
            at android.view.ViewRootImp

我不知道问题出在哪里。人们在 stackoverflow 上发布的大多数问题通常都涉及缺少的构造函数。我把它们都包括在内,所以我不确定问题是什么。

编辑:这也可能是相关的,我的 attrs.xml 文件。

 <declare-styleable name="RobotoTextView">
        <attr name="typeface"/>
    </declare-styleable>

    <attr name="typeface" format="enum">
        <enum name="roboto_thin" value="0"/>
        <enum name="roboto_thin_italic" value="1"/>
        <enum name="roboto_light" value="2"/>
        <enum name="roboto_light_italic" value="3"/>
        <enum name="roboto_regular" value="4"/>
        <enum name="roboto_italic" value="5"/>
        <enum name="roboto_medium" value="6"/>
        <enum name="roboto_medium_italic" value="7"/>
        <enum name="roboto_bold" value="8"/>
        <enum name="roboto_bold_italic" value="9"/>
        <enum name="roboto_black" value="10"/>
        <enum name="roboto_black_italic" value="11"/>
        <enum name="roboto_condensed" value="12"/>
        <enum name="roboto_condensed_italic" value="13"/>
        <enum name="roboto_condensed_bold" value="14"/>
        <enum name="roboto_condensed_bold_italic" value="15"/>
    </attr>

最佳答案

事实证明,某些 ttf 文件的名称已更改。 RobotoTextView 类在尝试查找某些文件时遇到问题。此外,我的 attrs.xml 文件的格式为“枚举”,而实际上它应该是整数。

关于java - Android 错误膨胀类 <unknown>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28388989/

相关文章:

java - 在menu.xml中使用actionLayout时android中的NullPointerException

xml - XSL 在 Google Chrome 中不起作用

android - 安卓外接触摸屏

java - 正确使用 Subscriber.onStart

java - 使用 JSON 和 XML 中的泛型的 Jersey Jackson 自定义响应

java - servlet 的 Hibernate 超时问题

java - hashCode 可以在不同的运行之间返回不同的值吗?

java - HashMap 的意外输出

java - 使用 Spring MVC 从存储库检索 XML 元数据

Android 2.2 联系人生日日期