运行时异常:字体找不到我尝试过的每个字体 - Android [英] Runtime Exception: Font not found for every font i've tried - Android

查看:1791
本文介绍了运行时异常:字体找不到我尝试过的每个字体 - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义扩展 TextView 我正在使用自定义字体在我的应用程序,但由于某种原因,我不断得到一个运行时异常程序不能找到有问题的字体。

我使用的目录格式是 main>资产>字体> Portrait-Light.ttf





我到处寻找一个解决方案,但他们似乎都围绕着同样的答案。
$ b CustomFontTextView.java

 公共类CustomFontTextView扩展TextView {
$ b $公共CustomFontTextView(上下文上下文){
super(context);
applyCustomFont(context);


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

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


private void applyCustomFont(Context context){
Log.e(It get here,custom font);
字体customFont = FontCache.getTypeface(Roboto-Italic.ttf,context);
setTypeface(customFont);




FontCache.java $ / p>

  class FontCache {

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

静态字体getTypeface(字体字体,上下文上下文){
字体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);
}
返回字体;




XML: p>

 < 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 color / colorPrimary
android:gravity =center_horizo​​ntal
android:text =@ string / are_you_over_18_years_old/>

我尝试过使用不同的格式,例如 .otf 和不同的字体,比如 Roboto-Italic.ttf 和dafont.com中另一个名为 Sunset-Clouds.ttf 但仍然收到错误信息,发生了什么事?这应该是工作。我甚至更新了所有插件,例如Gradle,Grade-Wrapper发行版和Android gradle插件以防万一。

我也尝试过这样做的单一方法: / p>

  AssetManager am = context。 getApplicationContext()。 getAssets(); 
Typeface font = Typeface.createFromAsset(
am,String.format(Locale.US,fonts /%s,portrait-light.ttf));

我错过了什么?

更新:



移除catch表示这个栈跟踪。 b
$ b


过程:icn.premierandroid,PID:3829
java.lang.RuntimeException:无法启动活动ComponentInfo {icn.premierandroid /icn.premierandroid.RegisterActivity}:
android.view.InflateException:二进制XML文件行#100:错误
在android.app.ActivityThread中膨胀类icn.premierandroid.misc.CustomFontTextView
。在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
.....

导致:android.view.InflateException:二进制XML文件行#100:
在android.view.LayoutInflater.createView(LayoutInflater.java:640)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
at android.view.Lay outInflater.rInflate(LayoutInflater.java:813)



.....



引起:java。 lang.reflect.InvocationTargetException $ b $在java.lang.reflect.Constructor.newInstance(本地方法)
在java.lang.reflect.Constructor.newInstance(Constructor.java:288)
在android .view.LayoutInflater.createView(LayoutInflater.java:614)



引起: java.lang.RuntimeException:找不到字体资产
fonts / GleegooRegular.ttf
at android.graphics.Typeface.createFromAsset(Typeface.java:272)
at icn.premierandroid.misc.FontCache .getTypeface(FontCache.java:21)
at icn.premierandroid.misc.CustomFontTextView.applyCustomFont(CustomFontTextView.java:28)
at icn.premierandroid.misc.CustomFontTextView。(CustomFontTextView.java:18)
(本地方法)



.....


更新2:



好的,所以有一个奇怪的解决方法。显然,当你将资产文件夹直接添加到主要文件夹中时,android studio不喜欢它C>应用程序/ SRC /主。你需要首先将它添加到 app / src / main / res 文件夹中,然后将它移动到 main 文件夹。没有一个线索,为什么它是这样的,但它解决了我的问题。

解决方案

我认为真正的问题是,你避难没有配置您的资产文件夹。如果你有,在项目视图中看起来像这样:





或者在Android视图中这样:





所以你应该在Studio中简单的添加这个文件夹作为资产文件夹:
点击你的项目结构窗口(或按Alt + 1),然后按Alt + Insert并选择Folder / Assets Folder。然后把你的字体放在那里。
$ b $ p从OP更新:

lockquote

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



I have a custom extended TextView I am using for custom fonts within my application, but for some reason I keep getting a run-time exception where the program can't find the font in question.

The format of the directory I'm using is main > assets > fonts > Portrait-Light.ttf

I've looked everywhere for a solution but they all seem to be rounding to the same answers on SO.

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

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

XML:

 <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"/>

I have tried this with different formats such as .otf and with different fonts such as Roboto-Italic.ttf and another random one from dafont.com called Sunset-Clouds.ttf but still i get the error message, what is going on? this should be working. I've even updated all plugins such as Gradle, Grade-Wrapper distribution and Android gradle plugin just in case.

I have also tried the single way of doing it:

    AssetManager am = context. getApplicationContext(). getAssets();
    Typeface font = Typeface.createFromAsset(
    am, String.format(Locale.US, "fonts/%s", "portrait-light.ttf"));

Am i missing something?

Update:

Removal of the catch reveals this stacktrace.

Process: icn.premierandroid, PID: 3829 java.lang.RuntimeException: Unable to start activity ComponentInfo{icn.premierandroid/icn.premierandroid.RegisterActivity}: android.view.InflateException: Binary XML file line #100: Error inflating class icn.premierandroid.misc.CustomFontTextView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) .....

Caused by: android.view.InflateException: Binary XML file line #100: Error inflating class icn.premierandroid.misc.CustomFontTextView at android.view.LayoutInflater.createView(LayoutInflater.java:640) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)

.....

Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:288) at android.view.LayoutInflater.createView(LayoutInflater.java:614)

....

Caused by: java.lang.RuntimeException: Font asset not found fonts/GleegooRegular.ttf at android.graphics.Typeface.createFromAsset(Typeface.java:272) at icn.premierandroid.misc.FontCache.getTypeface(FontCache.java:21) at icn.premierandroid.misc.CustomFontTextView.applyCustomFont(CustomFontTextView.java:28) at icn.premierandroid.misc.CustomFontTextView.(CustomFontTextView.java:18) at java.lang.reflect.Constructor.newInstance(Native Method) 

.....

UPDATE 2:

Okay, so there is a weird work-around for this. Apparently android studio doesn't like it when you add the assets folder straight into the main folder inside app/src/main. You need to add it into the app/src/main/res folder first and then move it to the main folder. Don't have a clue why it's like this but it solved my problem.

解决方案

I think the real problem is that you haven't configured your assets folder. If you had, it would look like this in Project view:

Or like this in Android view:

So you should simply add this folder as assets folder in Studio: Click on your project structure window (or press Alt+1), then press Alt + Insert and select Folder/Assets Folder. Then put your fonts there.

Update from OP:

Okay, so there is a weird work-around for this. Apparently android studio doesn't like it when you add the assets folder straight into the main folder inside app/src/main. You need to add it into the app/src/main/res folder first and then move it to the main folder. Don't have a clue why it's like this but it solved my problem.

这篇关于运行时异常:字体找不到我尝试过的每个字体 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆