Android 为什么会出现错误';无法在自定义视图中实例化以下类';是用安卓系统显示的吗?

Android 为什么会出现错误';无法在自定义视图中实例化以下类';是用安卓系统显示的吗?,android,android-layout,Android,Android Layout,在我的应用程序中,我使用扩展相对类创建视图(对于文本视图,我在java类中扩展本机文本视图),因为我想提供自定义字体,而不想在每个单独的文本视图中应用字体。为此,我使用下面的代码 public class LoyaltyTextView extends TextView { private final static String DROID_SANS = "droid_sans"; public LoyaltyTextView(Context context) { sup

在我的应用程序中,我使用扩展相对类创建视图(对于文本视图,我在java类中扩展本机文本视图),因为我想提供自定义字体,而不想在每个单独的文本视图中应用字体。为此,我使用下面的代码

public class LoyaltyTextView extends TextView {
    private final static String DROID_SANS = "droid_sans";
    public LoyaltyTextView(Context context) {
    super(context);
    }

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

public LoyaltyTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    parseAttributes(context, attrs);
}

private void parseAttributes(Context context, AttributeSet attrs) {
    TypedArray values = context.obtainStyledAttributes(attrs,R.styleable.LoyaltyTextView);
    String typeface = values.getString(R.styleable.LoyaltyTextView_customTypeFace);
    if(typeface.equalsIgnoreCase(DROID_SANS)) {
        setTypeface(Typeface.createFromAsset(context.getAssets(), "DroidSans.ttf"));
    }

    values.recycle();
}
}

我在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="match_parent"
android:background="@drawable/bg_splash">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_above="@+id/tv_logo"
                android:layout_centerHorizontal="true"
                android:src="@drawable/logo_m" />

            <customconstrols.LoyaltyTextView
                android:id="@+id/tv_logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="20dp"
                android:text="@string/loyalty_logo"
                android:textSize="@dimen/logo_text_size"
                android:textStyle="bold"
                app:customTypeFace="DROID_SANS" />

        </RelativeLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <com.customconstrols.LoyaltyTextView
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:text="@string/login"
                />
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

但当我使用简单的文本视图(作为本机android小部件)时,它可以正常工作,但当我使用上面的自定义控件预览部分开始给我错误,如图所示


我尝试了很多在不同帖子中提到的解决方案,比如清除缓存/失效选项,刷新布局和清理项目,并重建它,但对我来说没有任何效果。我缺少什么吗?

将代码替换为xml中的以下行

xmlns:font=”http://schemas.android.com/apk/res-auto"

运行你的应用程序,它会正常工作

public class MyFontTextView extends TextView {

    private Typeface typeface;

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

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

    public MyFontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }

    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.app);
        String customFont = a.getString(R.styleable.app_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }

    private boolean setCustomFont(Context ctx, String asset) {
        try {
            if (typeface == null) {
                typeface = Typeface.createFromAsset(ctx.getAssets(),
                        "fonts/OpenSans-Regular.ttf");
            }

        } catch (Exception e) {
            AndyUtils.generateLog("Exception in component"+e);
            return false;
        }

        setTypeface(typeface);
        return true;
    }
}
values->attr.xml中

<declare-styleable name="app">
        <attr name="customFont" format="string" />
</declare-styleable>


试试这个

在视图处于编辑模式时使用异常提示-跳过设置字体。我也尝试过:(.我已经提交了代码,设置类型面仍然有相同的错误。每当我做少量更改时,这个弹出窗口就会出现,以在java类中设置自定义字体。通过这种方式,我必须在每个类的每个文本视图中设置字体。假设10个活动和每个活动的文本视图数量都超过它这是一个很长的过程。我们还有其他短的替代方案吗?它不能在android studio工具中以xml启动。但它可以与应用程序相结合。