首页 文章

setText与自定义字体崩溃

提问于
浏览
0

我在assets文件夹/assets/fonts/7led.ttf中设置了自定义字体,并为文本视图初始化了字体 .

// LED font used in Dimmer display
Typeface tf = Typeface.createFromAsset(this.getAssets(), "fonts/7led.ttf");  
TextView tv  = (TextView) findViewById(R.id.DimmerView);    
tv.setTypeface(tf);

在xml文件中我将文本设置为默认值“100”

<TextView
    android:id="@+id/DimmerView"
    android:layout_width="169dp"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="700dp"
    android:background="@color/text_color"
    android:text="100"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/background_color"
    android:textSize="100dp" />

字体显示正确,但当我尝试更改文本时,它崩溃了 .

TextView.setText("101");

我已阅读了该主题的许多答案,但仍无法解决崩溃问题 . 我看到的许多错误之一是退出未捕获的异常 .

logcat 09-09 14:05:47.851:W / dalvikvm(3729):threadid = 1:线程退出时未捕获异常(组= 0x414422a0)

我已经解决了崩溃问题 .

Typeface tf = Typeface.createFromAsset(this.getAssets(), "fonts/digital_counter_7.ttf");  
final TextView tv  = (TextView) findViewById(R.id.DimmerView);      // for display the received data from the Arduino

tv.setText("101"); 
tv.setTypeface(tf);

此行可以更改文本

tv.setText("102");

2 回答

  • 0

    我想你调用文件夹时犯了一个小错误:

    如果您将.ttf文件放在/assets/folder/7led.ttf上,则需要调用

    Typeface tf = Typeface.createFromAsset(this.getAssets(), "folder/7led.ttf");
    

    并不是

    Typeface tf = Typeface.createFromAsset(this.getAssets(), "fonts/7led.ttf");
    
  • 0

    我的错误只是一个错字,“tff”而不是“ttf” . 检查一下,以防万一让你头疼 .

相关问题