2015-06-20 80 views
2

朋友 我正在使用DocumentView来验证和使用自定义字体。 我工作正确的字体,但是当我把自定义字体的路径放在它中。它显示错误给我。使用自定义字体时,DocumentView显示错误

这是我的项目。

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

这是我的错误堆栈。

Process: com.example.xperts98.customfonttest, PID: 11279 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.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) 

回答

2

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

下面是说明过程的图片。

enter image description here

其次你有“资产”文件夹下创建的“字体”文件夹中。 下面是说明程序的图片。 enter image description here

再次,您已将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_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     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