Unable to add custom font in RecyclerView.ViewHolder

浪尽此生 提交于 2019-12-06 09:27:38

问题


I am working on an android project and I added custom fonts on few layouts successfully. But when I tried adding them in a class which is not an activity which extends RecyclerView.ViewHolder, it creates a null pointer exception. I tried looking for but no luck. Initially I did not use context.getAssets(), I only used getAssets(). But then I was getting "cannot resolve method" on getAssets(). Since getAssets() is a part of Context, I included context and then all the errors went away. But when I run the app I get the runtime exception. I thinking it should something simple but I am sure I am missing something. Any help will be greatly appreciated.

Thanks in advance.

Here is the code

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);
}

}

And the exception

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:


Your context is not set at the time you're calling it...

Try this way:

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

This must be inside the constructor.



来源:https://stackoverflow.com/questions/28837181/unable-to-add-custom-font-in-recyclerview-viewholder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!