Pregunta

I use a custom Layout for displaying my ListView Items. But when I try to set font at RunTime app crashes. First I tried to access the Texviews of the layout in onCreate... seeing that it wont work, I looked for answers online and found out that I should be doing the setting in my custom Adapter. I tried that too, same result.

My adapter looks like this:

public class VizitaAdapter extends ArrayAdapter<Vizita>{
    Context context;
    int layoutResourceId;   
    Typeface fontux = null;

  public VizitaAdapter(Context context, int layoutResourceId, Vizita[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        fontux = Typeface.createFromAsset(context.getAssets(), "fonts/myrprocond.otf"); 
    }


 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;
 ...
 ...
        ((TextView)row).setTypeface(fontux);
        return row;
 }

So when my activity should start, I receive an error message that crashes the app. Before this, the app worked fine. What am I doing wrong? I should be able to set different textViews to different TypeFaces. That is actually what I need. But I cannot even set all textViews at once...

Please help. Thank you

UPDATE Logs are bellow

08-01 16:57:22.727: D/szipinf(881): Initializing inflate state 08-01 16:57:22.787: D/AndroidRuntime(881): Shutting down VM 08-01 16:57:22.787: W/dalvikvm(881): threadid=1: thread exiting with uncaught exception (group=0x40015560) 08-01 16:57:22.797: E/AndroidRuntime(881): FATAL EXCEPTION: main 08-01 16:57:22.797: E/AndroidRuntime(881): java.lang.ClassCastException: android.widget.RelativeLayout 08-01 16:57:22.797: E/AndroidRuntime(881): at softwarex.ssmmonitor_custom.VizitaAdapter.getView(VizitaAdapter.java:50) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.AbsListView.obtainView(AbsListView.java:1430) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.ListView.makeAndAddView(ListView.java:1745) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.ListView.fillDown(ListView.java:670) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.ListView.fillFromTop(ListView.java:727) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.ListView.layoutChildren(ListView.java:1598) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.AbsListView.onLayout(AbsListView.java:1260) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.view.View.layout(View.java:7175) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.view.View.layout(View.java:7175) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.view.View.layout(View.java:7175) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.view.View.layout(View.java:7175) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.view.View.layout(View.java:7175) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.os.Handler.dispatchMessage(Handler.java:99) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.os.Looper.loop(Looper.java:123) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.app.ActivityThread.main(ActivityThread.java:3683) 08-01 16:57:22.797: E/AndroidRuntime(881): at java.lang.reflect.Method.invokeNative(Native Method) 08-01 16:57:22.797: E/AndroidRuntime(881): at java.lang.reflect.Method.invoke(Method.java:507) 08-01 16:57:22.797: E/AndroidRuntime(881): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 08-01 16:57:22.797: E/AndroidRuntime(881): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-01 16:57:22.797: E/AndroidRuntime(881): at dalvik.system.NativeStart.main(Native Method) 08-01 16:57:24.657: I/Process(881): Sending signal. PID: 881 SIG: 9

I have no idea how to format the log to look more readable :(

Also I changed the Code above to reflect the latest try (which also gave me this error)

¿Fue útil?

Solución

Please, attach log. Most likely you have an NullPointerException on this line:

Typeface face=Typeface.createFromAsset(getAssets(), "fonts/yourfont.ttf"); 
((TextView)row).setTypeface(face);

convertView may be null and you should create a new one in this case or call the method getView of the super class(link)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top