gpt4 book ai didi

Android 应用程序在 Typeface.createFromAsset 上崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 08:34:58 27 4
gpt4 key购买 nike

我的应用程序因以下代码而崩溃。我正在尝试在 textView 中提供自定义字体。 Assets 目录没问题,因为我已经仔细检查过了。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView title = (TextView) findViewById(R.id.textView);
String BANGLA_FONT_SOLEMAN_LIPI = "/SolaimanLipi_Bold_10-03-12.ttf";
Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),
BANGLA_FONT_SOLEMAN_LIPI);
title.setTypeface(tf);
}

当应用程序崩溃时,它会产生以下错误日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gsl.banglafonttest/com.gsl.banglafonttest.MainActivity}: java.lang.RuntimeException: native typeface cannot be made
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.access$600(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5409)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: native typeface cannot be made
at android.graphics.Typeface.<init>(Typeface.java:175)
at android.graphics.Typeface.createFromAsset(Typeface.java:149)
at com.gsl.banglafonttest.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2416) 
at android.app.ActivityThread.access$600(ActivityThread.java:174) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5409) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606) 
at dalvik.system.NativeStart.main(Native Method) 

最佳答案

基本上有 4 种情况会导致这种情况:

1.你用错了扩展名

2.你把字体放在assets文件夹里而不是assets/fonts/

3.你把字体拼错了

4.字体需要小写(在我的例子中,解决方案是将 MyFont.ttf 重命名为 myfont.ttf,奇怪)

关于Android 应用程序在 Typeface.createFromAsset 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37519068/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com