自定义TextView

时间:2016-09-30 07:50:00

标签: android-layout android-custom-view

我有一个特定字体的自定义文本视图类。

我的班级:`

public LobsterTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    Typeface face=Typeface.createFromAsset(context.getAssets(), "lobster1_4.otf");
    this.setTypeface(face);
}

public LobsterTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    Typeface face=Typeface.createFromAsset(context.getAssets(), "lobster1_4.otf");
    this.setTypeface(face);
}

}

我的布局:

 <x.y.z.LobsterTextView
        android:id="@+id/tv_mobile_name"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="@dimen/font_large3"
        android:layout_marginTop="@dimen/fab_margin"
        android:layout_marginLeft="@dimen/fab_margin"
        android:textColor="@color/colorAccent_3"
        android:text="Register with Pika" />

和错误:`渲染问题无法实例化以下类: - io.pika.pike_store.utils.LobsterTextView(Open Class,Show Exception,Clear Cache)  提示:在android.graphics上的android.graphics.Typeface.createAssetUid(Typeface.java:219)的IDE异常详细信息java.lang.NullPointerException中显示时,在自定义视图中使用View.isInEditMode()来跳过代码或显示示例数据位于android.view的java.lang.reflect.Constructor.newInstance(Constructor.java:423)的io.pika.pike_store.utils.LobsterTextView。(LobsterTextView.java:22).Typeface.createFromAsset(Typeface.java:193) .LayoutInflater.createViewFromTag(LayoutInflater.java:727)位于android.view.LayoutInflater.RInflate(LinoutInflater.RInflate(LinoutInflater.Rava)中的android.view.LayoutInflater.DeInflate_Original(LayoutInflater.java:588)处于android.view.LayoutInflater.rInflate( LayoutInflater.java:834)在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)的android.view.LayoutInflater.RinoutInflater_Deleter.rInflate(LayoutInflater_Delegate.java:70)中的android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) )在androi在android.view.LayoutInflater的android.view.LayoutInflater_Delegate.parseInclude(LayoutInflater_Delegate.java:197)的android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)中的d.view.LayoutInflater.rInflate(LayoutInflater.java:834) .parseInclude(LayoutInflater.java:902)位于android.view.LayoutInflater.RInflate(LayoutInflater.Rava:70)中android.view.LayoutInflater.RInflate(LayoutInflater.Rava:70)处于android.view.LayoutInflater.rInflate(LayoutInflater:。 java:834)在android.view.LayoutInflater.inflate(LayoutInflater.java:518)的android.view.LayoutInflater.inflate(LayoutInflater.java:397)中的android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)堆栈到剪贴板LobsterTextView自定义视图比上一次构建更新编辑:构建项目。

我的代码一旦运行正常。但是当我来布局文件时,这个错误总是出现在前景中。

我该如何解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

公共类PahariTextView扩展了TextView {

public PahariTextView(Context context) {
    super(context);

    applyCustomFont(context);
}

public PahariTextView(Context context, AttributeSet attrs) {
    super(context, attrs);

    applyCustomFont(context);
}

public PahariTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    applyCustomFont(context);
}

private void applyCustomFont(Context context) {
    Typeface customFont = FontCache.getTypeface("fonts/quicksand_light.otf", context);
    setTypeface(customFont);
}

}