Android Galxy Tab Runtime异常:无法创建本机字体?

| 我正在尝试使用自定义字体 它可以在模拟器上正常工作。 但是在三星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???
    
已邀请:
我让它(恰好在Galaxy Tab上)做的差不多是您正在做的事情。原来对我来说是区分大小写的问题,例如文件名全部为小写,我在Java代码中将.ttf文件名大写。 因此,大概意味着这意味着在找不到ttf时都会出现此错误(因此请检查您的路径是否正确)。     
我遇到了同样的问题,我不认为这取决于设备。 我通过确保以下几点解决了它: 如果您有多个项目,请确保将字体文件存储在主项目(而不是从属项目)的资产文件夹中。 为了安全起见,将您的字体重命名为所有小写字母,并在代码中引用它。
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);
        }
    }
}
}
    

要回复问题请先登录注册