我正在尝试以编程方式更改textView的字体,我的应用程序一直在崩溃。这就是我的做法:
//SET CUSTOM FONT FOR TIMER
String fontPath = "fonts/voice_notepad_font.ttf";
TextView timer = (TextView)findViewById(R.id.timer);
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
timer.setTypeface(tf);
logcat的:
08-13 23:36:44.079: E/AndroidRuntime(11027): FATAL EXCEPTION: main
08-13 23:36:44.079: E/AndroidRuntime(11027): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whizzappseasyvoicenotepad/com.whizzappseasyvoicenotepad.TabLayout}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whizzappseasyvoicenotepad/com.whizzappseasyvoicenotepad.MainActivity}: java.lang.RuntimeException: native typeface cannot be made
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.os.Looper.loop(Looper.java:137)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-13 23:36:44.079: E/AndroidRuntime(11027): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 23:36:44.079: E/AndroidRuntime(11027): at java.lang.reflect.Method.invoke(Method.java:525)
08-13 23:36:44.079: E/AndroidRuntime(11027): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-13 23:36:44.079: E/AndroidRuntime(11027): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-13 23:36:44.079: E/AndroidRuntime(11027): at dalvik.system.NativeStart.main(Native Method)
08-13 23:36:44.079: E/AndroidRuntime(11027): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whizzappseasyvoicenotepad/com.whizzappseasyvoicenotepad.MainActivity}: java.lang.RuntimeException: native typeface cannot be made
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2054)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.widget.TabHost.setCurrentTab(TabHost.java:413)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.widget.TabHost.addTab(TabHost.java:240)
08-13 23:36:44.079: E/AndroidRuntime(11027): at com.whizzappseasyvoicenotepad.TabLayout.onCreate(TabLayout.java:33)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.app.Activity.performCreate(Activity.java:5133)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-13 23:36:44.079: E/AndroidRuntime(11027): ... 11 more
08-13 23:36:44.079: E/AndroidRuntime(11027): Caused by: java.lang.RuntimeException: native typeface cannot be made
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.graphics.Typeface.<init> (Typeface.java:175)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.graphics.Typeface.createFromAsset(Typeface.java:149)
08-13 23:36:44.079: E/AndroidRuntime(11027): at com.whizzappseasyvoicenotepad.MainActivity.onCreate(MainActivity.java:59)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.app.Activity.performCreate(Activity.java:5133)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-13 23:36:44.079: E/AndroidRuntime(11027): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-13 23:36:44.079: E/AndroidRuntime(11027): ... 21 more
我将自定义字体添加到assets / fonts文件夹并将其重命名为'voice_notepad_font'
答案 0 :(得分:1)
刚发现问题!我不知道为什么,但.ttf的情况很好(voice_notepad_font.TTF),这就是崩溃的原因。
我刚刚将String fontPath更改为:
String fontPath = "fonts/voice_notepad_font.TTF"
现在它完美无缺。
以前从来没有发生过这样的事实,每次下载自定义字体时,.ttf都是小写的。这就是为什么我不知道自己做错了什么。