2016-07-29 78 views
0

我想更改android內的textview的字體。在Android中更改字體使應用程序崩潰?

下面是代碼:

TextView textView = (TextView) findViewById(R.id.textView); 
    Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Thin.ttf"); 
    textView.setTypeface(font); 

,我把字體在src/main/assets/fonts文件夾中。

但是,當我嘗試運行應用程序(在模擬器上)它崩潰。這是爲什麼?

編輯:這是我得到的錯誤信息。

E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: com.asus.wetr, PID: 3973 
       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.asus.wetr/com.asus.wetr.MainActivity}: java.lang.RuntimeException: Font asset not found fonts/Roboto-Thin.TTF 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
       Caused by: java.lang.RuntimeException: Font asset not found fonts/Roboto-Thin.TTF 
        at android.graphics.Typeface.createFromAsset(Typeface.java:190) 
        at com.asus.wetr.MainActivity.onCreate(MainActivity.java:23) 
        at android.app.Activity.performCreate(Activity.java:6237) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
        at android.app.ActivityThread.-wrap11(ActivityThread.java)  
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
        at android.os.Handler.dispatchMessage(Handler.java:102)  
        at android.os.Looper.loop(Looper.java:148)  
        at android.app.ActivityThread.main(ActivityThread.java:5417)  
        at java.lang.reflect.Method.invoke(Native Method)  
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
Application terminated. 

EDIT2:這裏是我的activity.xml的請求。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.asus.wetr.MainActivity" 
android:background="@drawable/background_color"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="238dp" 
    android:id="@+id/textView" 
    android:textAppearance="@style/TextAppearance.AppCompat.Display1" 
    android:textColor="#FFF"/> 
</RelativeLayout> 
+1

提供實際的故障轉儲/錯誤。 – alzee

+0

發佈你正在得到的錯誤! – Shank

+0

請刪除'assets'中的'fonts'目錄,並在'createFromAsset'方法中更改路徑。 – jakubbialkowski

回答

1

我設法弄清了我自己的問題。夥計們,我想感謝你們所有的投入。我做了一個小的(我是一個新手)這個錯誤,它把所有東西搞砸了。

我把這個代碼:

TextView textView = (TextView) findViewById(R.id.textView); 
    Typeface thisfont = Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf"); 
    textView.setTypeface(thisfont); 

在此之前:

 setContentView(R.layout.activity_main); 

這是不行的(明顯對大多數的你爲什麼)因爲佈局尚未建立呢。它必須在之後。學過的知識。

3

你忘了傳遞TextView語境createFromAsset方法TypeFace

用途:

Typeface tf = Typeface.createFromAsset(textView.getContext() 
      .getAssets(), "fonts/Roboto-Thin.ttf"); 

我希望這可以幫助你。

+0

我做了,但現在我得到這個錯誤:'java.lang.NullPointerException:嘗試調用虛擬方法'android.content.Context android.widget.TextView.getContext()'on一個空對象引用',有什麼想法嗎? –

+0

發佈你的activity.xml請 –

+0

我現在在edit2下做了,請檢查。 –

1

展望堆棧跟蹤:

Font asset not found fonts/Roboto-Thin.TTF

它看起來像你的文件名是不同的。即使是文件擴展名爲的情況。那麼,你確定文件名和擴展名一樣嗎?

+0

是的,我100%確定,我甚至右鍵點擊字體(在Android Studio項目導航中)並複製引用。我也照你所說的做了,我刪除了字體目錄並將其更改爲createFromAsset()方法中的「Roboto-Thin.ttf」。也嘗試了不同的字體,它仍然沒有工作。我不知道接下來要嘗試什麼? :( –