【发布时间】:2015-05-30 17:41:51
【问题描述】:
我正在尝试在我的应用程序中使用自定义字体,但在运行应用程序时出现异常:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference
我有一个工作字体,它在正确的路径中,这就是我的代码:
private TextView tv;
private Typeface 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">
<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>
【问题讨论】:
-
你能发布
onCreate()代码吗?
标签: android nullpointerexception android-assets