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

时间:2016-05-30 06:35:15

标签: android

我的应用程序崩溃了以下代码。我试图在textView中提供自定义字体。资产目录很好,因为我仔细检查了它。

@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 个答案:

答案 0 :(得分:1)

基本上有4件事可能导致这种情况:

1.您使用了错误的扩展名

2.您将字体放在assets文件夹中,而不是在assets / fonts /

3.你拼错了字体

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

答案 1 :(得分:1)

字体tf = Typeface.createFromAsset(context.getAssets(),“font / Nunito-Light.ttf”);                 vh.textView_header.setTypeface(TF,45);

这是我在我的应用程序中使用的示例,自定义字体位于android资源目录中的字体文件夹中。

答案 2 :(得分:1)

我建议您Calligraphy摆脱自定义TextView或“样板”.setTypeFace

我曾经有自定义TextView,但书法使我的代码更清晰。

答案 3 :(得分:0)

我的解决方案是在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 = "font/SolaimanLipi_22-02-2012.ttf";
    Context cn=getApplicationContext();
    Typeface tf = Typeface.createFromAsset(cn.getAssets(),
            BANGLA_FONT_SOLEMAN_LIPI);
    title.setTypeface(tf);
    title.setText(R.string.text_of_title);
}

}