我正在尝试将TextView的字体更改为自定义字体。已阅读this other question并尝试了说明as outlined here。有问题的字体是Roboto Thin,直接从Android Design下载并且在 ttf 。
主要活动类的代码如下:
TextView txt = (TextView) findViewById(R.id.hometext);
Typeface font = Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");
txt.setTypeface(font);
// "hometext" is the TextView I wish to change font of.
当评论上面3行时,应用程序不会崩溃,我希望能够隔离问题。
其他等信息:
还试过“fonts / Roboto-Thin.ttf”和“assets / fonts / Roboto-Thin.ttf”,两者都不起作用。
01-17 22:38:29.086: D/AndroidRuntime(17774): Shutting down VM
01-17 22:38:29.086: W/dalvikvm(17774): threadid=1: thread exiting with uncaught exception (group=0x415f4ba8)
01-17 22:38:29.106: E/AndroidRuntime(17774): FATAL EXCEPTION: main
01-17 22:38:29.106: E/AndroidRuntime(17774): Process: sp.com, PID: 17774
01-17 22:38:29.106: E/AndroidRuntime(17774): java.lang.RuntimeException: Unable to start activity ComponentInfo{sp.com/sp.com.POSServices}: java.lang.RuntimeException: native typeface cannot be made
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.os.Handler.dispatchMessage(Handler.java:102)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.os.Looper.loop(Looper.java:136)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-17 22:38:29.106: E/AndroidRuntime(17774): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 22:38:29.106: E/AndroidRuntime(17774): at java.lang.reflect.Method.invoke(Method.java:515)
01-17 22:38:29.106: E/AndroidRuntime(17774): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-17 22:38:29.106: E/AndroidRuntime(17774): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-17 22:38:29.106: E/AndroidRuntime(17774): at dalvik.system.NativeStart.main(Native Method)
01-17 22:38:29.106: E/AndroidRuntime(17774): Caused by: java.lang.RuntimeException: native typeface cannot be made
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.graphics.Typeface.<init>(Typeface.java:175)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.graphics.Typeface.createFromAsset(Typeface.java:149)
01-17 22:38:29.106: E/AndroidRuntime(17774): at sp.com.POSServices.onCreate(POSServices.java:18)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.Activity.performCreate(Activity.java:5231)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-17 22:38:29.106: E/AndroidRuntime(17774): ... 11 more
答案 0 :(得分:1)
好的,android中有一个错误!这应该有效!
public class Typefaces {
private static final String TAG = "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) {
Log.e(TAG, "Could not get typeface '" + assetPath
+ "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
}
现在只需致电Typefaces.get(this,"address of font here")
我引用here.
祝你好运!
答案 1 :(得分:0)
看看你的字体在哪里。如果是资产/字体,则字体代码应为:
Typeface.createFromAsset(getAssets(), "fonts/Roboto-Thin.ttf");
答案 2 :(得分:0)
您应首先搜索相关问题,因为它表示无法创建TypeFace。你有没有给出正确的道路?如果是这样,也要测试其他字体。
之前的同样问题: "Native typeface cannot be made" only for some people
另外,请使用适用于Android的Fontify Library。它使一切变得更容易和更好!