无法在RecyclerView.ViewHolder中添加自定义字体

时间:2015-03-03 16:42:01

标签: android android-layout android-activity android-recyclerview android-fonts

我正在开发一个Android项目,并且我在几个布局上成功添加了自定义字体。但是当我尝试在一个不是扩展RecyclerView.ViewHolder的活动的类中添加它们时,它会创建一个空指针异常。我试着寻找但没有运气。最初我没有使用context.getAssets(),我只使用了getAssets()。但后来我得到了#34;无法解决方法"在getAssets()上。由于getAssets()是Context的一部分,因此我包含了上下文,然后所有错误都消失了。但是当我运行应用程序时,我得到了运行时异常。我认为这应该是简单的,但我确信我错过了一些东西。任何帮助将不胜感激。

提前致谢。

这是代码

public class TestLayoutViewHolder extends RecyclerView.ViewHolder {
protected TextView retName;
protected TextView messageHolder;


Context context;
Typeface tf_regular = Typeface.createFromAsset(context.getAssets(), "fonts/Lato-Regular.ttf");

public TestLayoutViewHolder(View view) {
    super(view);


    this.retailerName = (TextView) view.findViewById(R.id.retailerName);


    this.schemeMessageHolder = (TextView)view.findViewById(R.id.schemeMsgTxt);

    this.retName.setTypeface(tf_regular);
    //this.messageHolder.setTypeface(tf_regular);
}

}

异常

03-03 16:29:42.339  25381-25381/com.test.Test E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.acssl.smartloyal.SavedCardsViewHolder.<init>(SavedCardsViewHolder.java:21)
        at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:4121)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3431)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1306)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:523)
        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1988)
        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2260)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2007)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
        at android.view.Choreographer.doCallbacks(Choreographer.java:591)
        at android.view.Choreographer.doFrame(Choreographer.java:561)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:10)

您在调用它时未设置上下文...

尝试这种方式:

tf_regular = Typeface.createFromAsset(view.getContext().getAssets(), "fonts/Lato-Regular.ttf");
this.retName.setTypeface(tf_regular);

这必须在构造函数中。