Question

this is the getView function of classBinderData extends BaseAdapter:

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

adding the line: Typeface tf = Typeface.createFromAsset(vi.getContext().getAssets(), "fonts/Roboto_Thin.ttf");

makes the app crash.

So how to set font?

Edit

LogCat

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) 
Was it helpful?

Solution

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 is an heavy operation. Do it once when you retrieve the TextView from your layout

Also be sure that inside the assets folder you have created the folder fonts and that it contains Roboto_Thin.ttf

OTHER TIPS

Firstly, don't allocate the typeface each time you create the view. Allocate once, and use that in every new view.

In that case, check out this topic, and try out the suggestions there: Null pointer exception while importing a font in android

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top