0

这是classBinderData扩展BaseAdapter的getView函数:

public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;

    if (convertView == null) {
                    vi = inflater.inflate(R.layout.list_row, null);
                    holder = new ViewHolder();
                    holder.name = (TextView) vi.findViewById(R.id.name); 
                   vi.setTag(holder);
    } else {
                    holder = (ViewHolder) vi.getTag();
    }

    Typeface tf = Typeface.createFromAsset(vi.getContext().getAssets(), "fonts/Roboto_Thin.ttf");
    holder.name.setTypeface(tf);    
    return vi;
}`

添加行:Typeface tf = Typeface.createFromAsset(vi.getContext().getAssets(), "fonts/Roboto_Thin.ttf");

使应用程序崩溃。

那么如何设置字体呢?

编辑

日志猫

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.myapp.BinderData.getView(BinderData.java:65)
at android.widget.AbsListView.obtainView(AbsListView.java:2045)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
at android.widget.ListView.onMeasure(ListView.java:1155)
at android.view.View.measure(View.java:12853)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4806)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1386)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:677)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:558)
at android.view.View.measure(View.java:12853)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4806)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:298)
at android.view.View.measure(View.java:12853)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:829)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:558)
at android.view.View.measure(View.java:12853)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4806)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:298)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2111)
at android.view.View.measure(View.java:12853)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1065)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2455)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method) 
4

2 回答 2

3
public View getView(int position, View convertView, ViewGroup parent) {

View vi = convertView;

if (convertView == null) {
    vi = inflater.inflate(R.layout.list_row, null);
    holder = new ViewHolder();
    holder.name = (TextView) vi.findViewById(R.id.name); 
    Typeface tf = Typeface.createFromAsset(vi.getContext().getAssets(), "fonts/Roboto_Thin.ttf");
    holder.name.setTypeface(tf);    

    vi.setTag(holder);
} else {
    holder = (ViewHolder) vi.getTag();
}

     return vi;
}

TypeFace.createFromAsset 是一个繁重的操作。TextView从布局中检索时执行一次

还要确保在 assets 文件夹中您已经创建了该文件夹fonts并且它包含Roboto_Thin.ttf

于 2013-06-02T12:03:28.147 回答
0

首先,不要在每次创建视图时分配字体。分配一次,并在每个新视图中使用它。

在这种情况下,请查看此主题,并尝试那里的建议: Null pointer exception while importing a font in android

于 2013-06-02T11:33:08.213 回答