Android Galxy Tab 运行时异常:无法制作原生字体?

发布于 2024-11-02 00:34:58 字数 505 浏览 0 评论 0原文

我正在尝试使用自定义字体 它可以在模拟器上运行,没有任何问题。

但在 Smsung Galaxy Tab 上抛出以下错误: 无法制作原生字体

这是我的代码:

               public static Typeface typeface;
             // -----define typeface

    typeface = Typeface.createFromAsset(getAssets(), "fonts/Verdana.TTf");
    Typeface.class.getField("DEFAULT").setAccessible(true);
                          ---------------------
        lblBrandCategory1.setTypeface(GuestActivity.typeface, 4);


            anyone knows the solution???

I'm trying to use custom fonts
It works on emulator with no isses.

But on Smsung Galaxy Tab is throwing following error:
native typeface cannot be made

Here is my code:

               public static Typeface typeface;
             // -----define typeface

    typeface = Typeface.createFromAsset(getAssets(), "fonts/Verdana.TTf");
    Typeface.class.getField("DEFAULT").setAccessible(true);
                          ---------------------
        lblBrandCategory1.setTypeface(GuestActivity.typeface, 4);


            anyone knows the solution???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

儭儭莪哋寶赑 2024-11-09 00:34:58

我的这个(在 Galaxy Tab 上)几乎和你正在做的一样。结果对我来说是一个区分大小写的问题,例如文件名全部小写,而我在java代码中将.ttf文件名大写。

因此,这可能意味着只要找不到 ttf,您就会收到此错误(因此请检查您的路径是否良好)。

I had this (on a Galaxy Tab as it happens) doing pretty much exactly what you're doing. Turned out to be a case-sensitivity issue for me, e.g. filename was all lowercase and I capitalized the .ttf filename in the java code.

So presumably that means that you get this error whenever the ttf can't be found (so check your path is good as well).

惯饮孤独 2024-11-09 00:34:58

我遇到了同样的问题,我不认为它依赖于设备。

我通过确保以下几点解决了这个问题:

  1. 如果您有多个项目,请确保您的字体文件存储在主项目的资产文件夹中 - 而不是依赖项目。< /p>

  2. 为了安全起见,请将字体重命名为全部小写,并在代码中引用它。

    FontUtils.setDefaultFont(this, "DEFAULT", "fonts/arimo-regular.ttf");

这是一个用于覆盖整个应用程序默认字体的类。

public class FontUtils {

/**
 * Sets the default font.
 *
 * @param context the context
 * @param staticTypefaceFieldName the static typeface field name
 * @param fontAssetName the font asset name
 */
public static void setDefaultFont(Context context,
        String staticTypefaceFieldName, String fontAssetName) {
    final Typeface regular = Typefaces.get(context, fontAssetName);
    replaceFont(staticTypefaceFieldName, regular);
}

/**
 * Replace a font.
 *
 * @param staticTypefaceFieldName the static typeface field name
 * @param newTypeface the new typeface
 */
protected static void replaceFont(String staticTypefaceFieldName,
        final Typeface newTypeface) {
    try {
        final Field StaticField = Typeface.class
                .getDeclaredField(staticTypefaceFieldName);
        StaticField.setAccessible(true);
        StaticField.set(null, newTypeface);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

static class Typefaces {

    private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

    public static Typeface get(Context c, String assetPath) {
        synchronized (cache) {
            if (!cache.containsKey(assetPath)) {
                try {
                    Typeface t = Typeface.createFromAsset(c.getAssets(),
                            assetPath);
                    cache.put(assetPath, t);
                } catch (Exception e) {
                    System.out.println("Could not get typeface '" + assetPath + "' because " + e.getMessage());
                    return null;
                }
            }
            return cache.get(assetPath);
        }
    }
}
}

I had the same issue, I don't believe it's device dependent.

I solved it by making sure of the following:

  1. If you have more than one project, make sure your font file is stored in the assets folder of your main project - not a dependency project.

  2. for safety, rename your font to all lowercase and reference it as such in your code.

    FontUtils.setDefaultFont(this, "DEFAULT", "fonts/arimo-regular.ttf");

Here is a class to override the default font for my entire application.

public class FontUtils {

/**
 * Sets the default font.
 *
 * @param context the context
 * @param staticTypefaceFieldName the static typeface field name
 * @param fontAssetName the font asset name
 */
public static void setDefaultFont(Context context,
        String staticTypefaceFieldName, String fontAssetName) {
    final Typeface regular = Typefaces.get(context, fontAssetName);
    replaceFont(staticTypefaceFieldName, regular);
}

/**
 * Replace a font.
 *
 * @param staticTypefaceFieldName the static typeface field name
 * @param newTypeface the new typeface
 */
protected static void replaceFont(String staticTypefaceFieldName,
        final Typeface newTypeface) {
    try {
        final Field StaticField = Typeface.class
                .getDeclaredField(staticTypefaceFieldName);
        StaticField.setAccessible(true);
        StaticField.set(null, newTypeface);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

static class Typefaces {

    private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

    public static Typeface get(Context c, String assetPath) {
        synchronized (cache) {
            if (!cache.containsKey(assetPath)) {
                try {
                    Typeface t = Typeface.createFromAsset(c.getAssets(),
                            assetPath);
                    cache.put(assetPath, t);
                } catch (Exception e) {
                    System.out.println("Could not get typeface '" + assetPath + "' because " + e.getMessage());
                    return null;
                }
            }
            return cache.get(assetPath);
        }
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文