gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:41:18 29 4
gpt4 key购买 nike

我正在开发一个 android 项目,我成功地在几个布局上添加了自定义字体。但是当我尝试将它们添加到一个不是扩展 RecyclerView.ViewHolder 的 Activity 的类中时,它会创建一个空指针异常。我试着寻找但没有运气。最初我没有使用 context.getAssets(),我只使用了 getAssets()。但后来我在 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);
}

还有异常(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)

最佳答案

你的上下文在你调用它的时候还没有设置...

试试这个方法:

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

这必须在构造函数中。

关于android - 无法在 RecyclerView.ViewHolder 中添加自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28837181/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com