DocumentView 在使用自定义字体时显示错误

Posted

技术标签:

【中文标题】DocumentView 在使用自定义字体时显示错误【英文标题】:DocumentView shows me error while using custom font 【发布时间】:2015-06-20 05:13:04 【问题描述】:

朋友 我正在使用 DocumentView 来证明并在其中使用自定义字体。 我可以很好地证明字体的合理性,但是当我将自定义字体的路径放入其中时。它向我显示错误。

这是我的项目。

http://www.mediafire.com/download/ggobgpzzxjc412m/CustomFontTest.zip

这是我的错误堆栈。

Process: com.example.xperts98.customfonttest, PID: 11279
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.xperts98.customfonttest/com.example.xperts98.customfonttest.MainActivity: android.view.InflateException: Binary XML file line #11: Error inflating class com.bluejamesbond.text.DocumentView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
        at `enter code here`android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
        at android.app.ActivityThread.access$800(ActivityThread.java:148)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
 Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.bluejamesbond.text.DocumentView
        at android.view.LayoutInflater.createView(LayoutInflater.java:633)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
        at com.example.xperts98.customfonttest.MainActivity.onCreate(MainActivity.java:17)
        at android.app.Activity.performCreate(Activity.java:5953)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
        at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.example.xperts98.customfonttest.MainActivity.onCreate(MainActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:5953)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
 Caused by: java.lang.RuntimeException: Font asset not found fonts/ProximaNova-Light.ttf
        at android.graphics.Typeface.createFromAsset(Typeface.java:190)
        at com.bluejamesbond.text.DocumentView.initDocumentView(DocumentView.java:315)
        at com.bluejamesbond.text.DocumentView.<init>(DocumentView.java:107)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.example.xperts98.customfonttest.MainActivity.onCreate(MainActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:5953)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

【问题讨论】:

【参考方案1】:

首先,您必须使用 android studio 向导在“main”文件夹中创建“assets”文件夹。

这是说明过程的图片。

其次,您必须在“assets”文件夹下创建“fonts”文件夹。 这是将说明该过程的图片。

第三,你已经把你的 font.ttf/font.otf 文件放在了字体文件夹中。

这里是 activity_main.xml 代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_
        android:layout_
        android:layout_gravity="center_horizontal"
        android:text="@string/my_text"
        android:textAppearance="?android:attr/textAppearanceLarge" />


</LinearLayout>

这里是 MainActivity.java 代码

package com.example.documentviewwithcustomfont;

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.bluejamesbond.text.DocumentView;
import com.bluejamesbond.text.style.TextAlignment;

public class MainActivity extends AppCompatActivity 

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textView);

        DocumentView documentView = new DocumentView(this, DocumentView.PLAIN_TEXT);
        documentView.getDocumentLayoutParams().setTextAlignment(TextAlignment.JUSTIFIED);

        Typeface font = Typeface.createFromAsset(getAssets(), "fonts/proxima_nova_regular.ttf");

        textView.setTypeface(font);


    


以下是您可以下载整个项目的链接。

http://www.mediafire.com/download/28726mwwhvua5nd/DocumentViewWithCustomFont.rar

【讨论】:

以上是关于DocumentView 在使用自定义字体时显示错误的主要内容,如果未能解决你的问题,请参考以下文章

如何自定义“关于应用程序”菜单项

使用Wix在MSI中自定义操作出错时显示最终用户消息

选择表格视图单元格时显示自定义 UIImageView

osmdroid:加载自定义tilesource时显示的乱码

为啥我在自定义单元格中的复选框在快速选择和滚动时显示不同的行为?

在 Android 中使用自定义字体