带有来自资产的自定义字体的 NullPointerException [英] NullPointerException with custom font from assets

查看:152
本文介绍了带有来自资产的自定义字体的 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中使用自定义字体,但在运行应用程序时出现异常:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference

我有一个可用的字体,它在正确的路径中,这就是我的代码:

私有TextView tv;私人字体 tf;tv = (TextView) findViewById(R.id.wlcText);tf = Typeface.createFromAsset(getAssets(), "en_font.ttf");tv.setTypeface(tf);

这是我的 XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><线性布局android:id="@+id/mainView"android:background="@color/semi_transparent_black"android:layout_width="match_parent"android:layout_height="wrap_content"机器人:方向=垂直"android:padding="@dimen/activity_vertical_margin"><文本视图android:id="@+id/wlcText"android:layout_width="fill_parent"android:layout_height="wrap_content"android:textColor="@android:color/white"机器人:文本=测试"android:textSize="25sp"/></LinearLayout>

解决方案

tv is null.无论出于何种原因(例如,未能调用 setContentView()),您在活动中都没有可通过 R.id.wlcText 找到的小部件.

I'm trying to use custom font in my application, but i got that exception when when run the app:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference

I have a working font and it's in the right path and that's my code:

private TextView tv;
private Typeface tf;

    tv = (TextView) findViewById(R.id.wlcText);
    tf = Typeface.createFromAsset(getAssets(), "en_font.ttf");
    tv.setTypeface(tf);

That's my XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/mainView"
    android:background="@color/semi_transparent_black"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/wlcText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:text="Test"
        android:textSize="25sp" />

</LinearLayout>

解决方案

tv is null. For whatever reason (e.g., failure to call setContentView()), you do not have a widget in the activity that can be found via R.id.wlcText.

这篇关于带有来自资产的自定义字体的 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆