Java Android Studio呈现问题,无法找到自定义TextView的类

Java Android Studio呈现问题,无法找到自定义TextView的类,java,android,android-layout,Java,Android,Android Layout,我遇到了渲染问题,还没有找到解决方法。我创建了一个自定义textview类,它将视图设置为自定义字体。我相信我的设置是正确的,但是由于某些原因,没有应用自定义文本,并且我在使用此自定义textview小部件的布局中收到渲染问题。这是我的设置 自定义文本视图类 public class FontedTextView extends TextView { public FontedTextView(Context context) { super(context);

我遇到了渲染问题,还没有找到解决方法。我创建了一个自定义textview类,它将视图设置为自定义字体。我相信我的设置是正确的,但是由于某些原因,没有应用自定义文本,并且我在使用此自定义textview小部件的布局中收到渲染问题。这是我的设置

自定义文本视图类

public class FontedTextView extends TextView {

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

    public FontedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        CustomFontHelper.setCustomFont(this, context, attrs);
    }

    public FontedTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        CustomFontHelper.setCustomFont(this, context, attrs);
    }
}
帮助器类来设置字体

class CustomFontHelper {

    public static void setCustomFont(TextView textview, Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFont);
        String font = a.getString(R.styleable.CustomFont_font);
        setCustomFont(textview, font, context);
        a.recycle();
    }

    private static void setCustomFont(TextView textview, String font, Context context) {
         if (font == null) {
            return;
        }

        Typeface tf = FontCache.get(font, context);
        if (tf != null) {
            textview.setTypeface(tf);
        }
    }
}
我在布局中使用的文本视图如下所示

        <com.example.gui.FontedTextView
            style="@style/CustomTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test new text typeface"
            android:textColor="@color/black"
            android:textSize="40sp" />

在styles.xml中,我添加了一个引用自定义字体的样式

<style name="CustomTextView" parent="@android:style/Widget.TextView">
    <item name="com.example:font">fonts/UglyFont.otf</item>
</style>

字体/UglyFont.otf
我的attrs.xml如下所示

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomFont">
        <attr name="font" format="string"/>
    </declare-styleable>
</resources>

这是我在其他线程中发现的一个问题,但我的特殊情况似乎有点不同。我的样式中没有引用AppCompact,因此我不需要添加任何支持库或依赖项。与我创建的自定义文本视图存在冲突。下面是错误说明的内容

无法实例化以下类:

-com.example.gui.FontedTextView(打开类,显示异常,清除缓存)

提示:在自定义视图中使用View.isInEditMode()跳过代码或显示 IDE异常详细信息中显示的示例数据 java.lang.NoClassDefFoundError:com/example/gui/CustomFontHelper 位于com.example.gui.FontedButton.(FontedButton.java:19) newInstance(Constructor.java:408)位于 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 在 android.view.LayoutInflater.rInflate_原件(LayoutInflater.java:835) 在 rayoutinflater_Delegate.rInflate(LayoutInflater_Delegate.java:70) 位于android.view.LayoutInflater.rInflate(LayoutInflater.java:811) 在 rayoutinflater.rInflateChildren(LayoutInflater.java:798) 在 android.view.LayoutInflater.rInflate_原件(LayoutInflater.java:838) 在 rayoutinflater_Delegate.rInflate(LayoutInflater_Delegate.java:70) 位于android.view.LayoutInflater.rInflate(LayoutInflater.java:811) 在 rayoutinflater.rInflateChildren(LayoutInflater.java:798) 在 android.view.LayoutInflater.rInflate_原件(LayoutInflater.java:838) 在 rayoutinflater_Delegate.rInflate(LayoutInflater_Delegate.java:70) 位于android.view.LayoutInflater.rInflate(LayoutInflater.java:811) 在 rayoutinflater.rInflateChildren(LayoutInflater.java:798) 在 android.view.LayoutInflater.rInflate_原件(LayoutInflater.java:838) 在 rayoutinflater_Delegate.rInflate(LayoutInflater_Delegate.java:70) 位于android.view.LayoutInflater.rInflate(LayoutInflater.java:811) 在 rayoutinflater.rInflateChildren(LayoutInflater.java:798) 在 android.view.LayoutInflater.rInflate_原件(LayoutInflater.java:838) 在 rayoutinflater_Delegate.rInflate(LayoutInflater_Delegate.java:70) 位于android.view.LayoutInflater.rInflate(LayoutInflater.java:811) 在 rayoutinflater.rInflateChildren(LayoutInflater.java:798) 在android.view.LayoutInflater.inflate(LayoutInflater.java:515)中 膨胀(LayoutInflater.java:394)

我试过了,但问题没有解决

--使缓存无效并重新启动

--重建/清理


--将SDK级别从23更改为21、20、19、18和17。没有任何区别

编译器似乎找不到CustomHelper CustomFontHelper.com。请在样式中将“com.example:font”替换为“font”。xml@WaleedSarwar我照你说的做了,字体还是没变。我不确定为什么找不到我的助手。类FontedTextView和CustomFontHelper位于同一个包中。我重构了CustomFontHelper,以查看FontedTextView中的引用是否发生了更改。还有其他建议吗?我认为这是IDE的问题。你使用的是最新版本的android Studio吗?因为我已经尝试过你的代码,它对meYea有效,甚至后来因为我在开发频道。使用1.4 RC2。但是我在1.3上也有这个问题。我不认为这是IDE问题,因为我想要应用的字体没有被应用。