Typeface.createFromAsset()返回字体的空指针异常

时间:2016-06-21 17:42:05

标签: java android nullpointerexception stack-trace

在我的应用程序的登录活动中,我将一个变量分配给我的资源文件夹中的字体。但是,当我运行应用程序时,我在logcat中得到一个堆栈跟踪,说该方法返回一个空指针异常。

这是我的LoginActivity.java

package com.joshhess.fittrak;

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginActivity extends AppCompatActivity {

    // UI Elements
    @BindView(R.id.title) TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        // Set font for widgets
        Typeface roboto = Typeface.createFromAsset(this.getAssets(), "fonts/Roboto-Regular.ttf");
        title.setTypeface(roboto);
    }
}

这是我的堆栈跟踪:

    06-21 13:37:37.298 27151-27151/com.joshhess.fittrak E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.joshhess.fittrak, PID: 27151
                                                                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.joshhess.fittrak/com.joshhess.fittrak.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2661)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                      at android.app.ActivityThread.access$900(ActivityThread.java:172)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:145)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5835)
                                                                      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:1399)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
                                                                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference
                                                                      at com.joshhess.fittrak.LoginActivity.onCreate(LoginActivity.java:24)
                                                                      at android.app.Activity.performCreate(Activity.java:6221)
                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                      at android.app.ActivityThread.access$900(ActivityThread.java:172) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:145) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5835) 
                                                                      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:1399) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

最后,这是activity_login.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    android:background="@color/colorPrimary"
    tools:context="com.joshhess.fittrak.LoginActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="56dp"
        android:paddingRight="24dp"
        android:paddingLeft="24dp">

        <TextView android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingTop="32dp"
            android:textColor="@color/colorAccent"
            android:textSize="55sp"
            android:text="@string/app_name" />

    </LinearLayout>

</ScrollView>

有没有人知道为什么它返回一个空引用?我的文件夹名为assets,文件名正确。

2 个答案:

答案 0 :(得分:2)

感谢Renan Bandeira,我意识到了我的问题。我的build.gradle文件未包含apt 'com.jakewharton:butterknife:8.1.0'。在编译行下添加该行后,它完全正常。以下是我build.gradle中的依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.jakewharton:butterknife:8.1.0'
    apt 'com.jakewharton:butterknife-compiler:8.1.0'
}

答案 1 :(得分:0)

字段标题仍为空。 所以当你打电话时

title.setTypeFace(robot)

然后它将是nullPointerException,因为您正在调用null对象上的方法。 我想

@BindView(R.id.title) TextView title; 

标题未初始化。 请调试它,对于标题值,您将得到什么是null的确切问题。可能是ButterKnife配置存在问题。

我在下面发现了类似的问题,可能它可以帮助您调试和修复它。 How to debug Null Pointer Exception raised by Butterknife in an Android app?