0

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) 
Frozen Crayon
  • 5,172
  • 8
  • 36
  • 71

2 Answers2

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 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

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

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

Community
  • 1
  • 1
Tamas
  • 221
  • 1
  • 8
  • Oops. But the font name is correct. Now where do I get this stack trace from, in Eclipse? – Frozen Crayon Jun 02 '13 at 11:43
  • You can see the error stack trace in the logcat window (Window > Show View > Other > Logcat). Filter this to your application, and the last lines written with red (starting with Uncaught exception: ) is the exception that caused the force close. In that you can find where and possibly why was the exception thrown. – Tamas Jun 02 '13 at 11:49
  • There you go, the logcat. I can't make heads or tails of it. – Frozen Crayon Jun 02 '13 at 12:12
  • Which is line 65? The one with creating the typeface, or the one setting it? – Tamas Jun 02 '13 at 12:15
  • Creating typeface from assets – Frozen Crayon Jun 02 '13 at 12:21