Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行时异常:未找到每个字体i';我试过了-安卓_Java_Android_Fonts_Android Assets_Custom Font - Fatal编程技术网

Java 运行时异常:未找到每个字体i';我试过了-安卓

Java 运行时异常:未找到每个字体i';我试过了-安卓,java,android,fonts,android-assets,custom-font,Java,Android,Fonts,Android Assets,Custom Font,我有一个自定义扩展的TextView我在应用程序中用于自定义字体,但由于某些原因,我一直收到一个运行时异常,程序无法找到有问题的字体 我正在使用的目录格式是main>assets>font>cratital Light.ttf 我到处寻找解决办法,但他们似乎都在寻找相同的答案 CustomFontTextView.java: public class CustomFontTextView extends TextView { public CustomFontTextView(Con

我有一个自定义扩展的
TextView
我在应用程序中用于自定义字体,但由于某些原因,我一直收到一个运行时异常,程序无法找到有问题的字体

我正在使用的目录格式是
main>assets>font>cratital Light.ttf

我到处寻找解决办法,但他们似乎都在寻找相同的答案

CustomFontTextView.java:

public class CustomFontTextView extends TextView {

    public CustomFontTextView(Context context) {
        super(context);
        applyCustomFont(context);
    }

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

    public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        applyCustomFont(context);
    }

    private void applyCustomFont(Context context) {
        Log.e("it gets here", "custom font");
        Typeface customFont = FontCache.getTypeface("Roboto-Italic.ttf", context);
        setTypeface(customFont);
    }
}
 <icn.premierandroid.misc.CustomFontTextView
            android:id="@+id/switch_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:textSize="14sp"
            android:textColor="@color/colorPrimary"
            android:gravity="center_horizontal"
            android:text="@string/are_you_over_18_years_old"/>
FontCache.java

class FontCache {

    private static HashMap<String, Typeface> fontCache = new HashMap<>();

    static Typeface getTypeface(String fontname, Context context) {
        Typeface typeface = fontCache.get(fontname);
        if (typeface == null) {
            try {
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + fontname);
            } catch (Exception e) {
                Log.e("failed", e.getMessage());
            }
            fontCache.put(fontname, typeface);
        }
        return typeface;
    }
}
我错过什么了吗

更新:

移除捕捉会显示此堆栈跟踪

进程:icn.1,PID:3829 java.lang.RuntimeException:无法启动活动组件信息{icn.premierandroid/icn.premierandroid.RegisterActivity}: android.view.InflateException:二进制XML文件行#100:错误 充气类icn.premierandroid.misc.CustomFontTextView 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)上 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)上

原因:android.view.InflateException:二进制XML文件行#100: 膨胀类icn.premierandroid.misc.CustomFontTextView时出错 位于android.view.LayoutInflater.createView(LayoutInflater.java:640) 位于android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 位于android.view.LayoutInflater.rInflate(LayoutInflater.java:813)

原因:java.lang.reflect.InvocationTargetException 位于java.lang.reflect.Constructor.newInstance(本机方法) 位于java.lang.reflect.Constructor.newInstance(Constructor.java:288) 位于android.view.LayoutInflater.createView(LayoutInflater.java:614)

原因:java.lang.RuntimeException:找不到字体资产 fonts/glegoregular.ttf 在android.graphics.Typeface.createFromAsset(Typeface.java:272)上 在icn.premierandroid.misc.FontCache.getTypeface(FontCache.java:21)上 在icn.premierandroid.misc.CustomFontTextView.applyCustomFont(CustomFontTextView.java:28)上 在icn.premierandroid.misc.CustomFontTextView.(CustomFontTextView.java:18) 位于java.lang.reflect.Constructor.newInstance(本机方法)

更新2:


好吧,这是一个奇怪的工作。显然,android studio不喜欢将
assets
文件夹直接添加到
app/src/main
中的
main
文件夹中。您需要先将其添加到
app/src/main/res
文件夹中,然后将其移动到
main
文件夹中。我不知道为什么会这样,但它解决了我的问题。

我们根据自己的需要编写了这个类。它允许使用属性设置自定义字体,如下所示:

app:customFont="Roboto-Medium.ttf"
希望您觉得它有用:

public class TextViewFonted extends TextView {
    private static final String TAG = "TextViewFonted";

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

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

    public TextViewFonted(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.TextViewFonted);
        String customFont = a.getString(R.styleable.TextViewFonted_customFont);
        if (customFont == null) customFont = "Roboto-Regular.ttf";
        setCustomFont(ctx, customFont);
        a.recycle();
    }

    public boolean setCustomFont(Context ctx, String asset) {
        Typeface tf = null;
        try {
            tf = Typeface.createFromAsset(ctx.getAssets(), asset);  
        } catch (Exception e) {
            Log.e(TAG, "Could not get typeface", e);
            return false;
        }

        setTypeface(tf);  
        setLineSpacing(getTextSize() * 0.3f, 0.75f);
        return true;
    }
}
为了能够使用此自定义属性,您应该在attrs.xml的下一个块中添加以下内容(因此R.styleable.TextViewFonted_customFont将起作用):


我认为真正的问题是您尚未配置资产文件夹。如果有,则在项目视图中如下所示:

或者像Android视图中的这样:

因此,您只需将此文件夹添加为Studio中的资产文件夹: 单击项目结构窗口(或按Alt+1),然后按Alt+Insert并选择文件夹/资源文件夹。然后把你的字体放在那里

从OP更新:

好吧,这是一个奇怪的工作。显然,android studio不喜欢将资产文件夹直接添加到app/src/main内的主文件夹中。您需要先将其添加到app/src/main/res文件夹中,然后将其移动到主文件夹中。我不知道为什么会这样,但它解决了我的问题

试试这个

put .ttf fonts in app > assets > fonts > Roboto-Italic.ttf
然后在onCreate方法中

Typeface Roboto = Typeface.createFromAsset(getAssets(),
            "fonts/Roboto-Italic.ttf");
并在文本视图中设置字体等

textview.setTypeface(Roboto);

请发布错误stacktrace@maxost,为您添加了它,bud。“Font asset not found fonts/Gleegoregular.ttf”,我不确定,但在您的字体目录中没有看到此字体。它是在之后添加的,抱歉,我在等待答案时调试了它(只是尝试一种新字体)。我会尝试一下,并让您知道,谢谢您的回答。很遗憾,它仍然在fonts目录和assets目录中为我提供“java.lang.RuntimeException:Font asset not found”。你确定你没有错报字体名吗?我百分之百地仔细检查了它。我也投了这个类的票,我使用了它,并将继续使用它。我喜欢attr功能。我确实是这样添加的,我会尝试删除它,然后再添加。我会让你知道结果的。我会试试看,然后告诉你。
put .ttf fonts in app > assets > fonts > Roboto-Italic.ttf
Typeface Roboto = Typeface.createFromAsset(getAssets(),
            "fonts/Roboto-Italic.ttf");
textview.setTypeface(Roboto);