0

I am trying to change font of listview elements. It is made with a custom listview. I am having error when trying to set font of listview text view. I have followed tutorials on this same site

but still getting errors. The error is I am referencing a null. The code has an error when I try to use viewfinder. If I remove the code simple cursor adapter.viewfinder the code works. I have created a class to call and return the font it works elsewhere. Below is the code for the function to set listview adapter.

private void displayListView() {
    //sqlite data helper class to receive all the data
        Cursor cursor = dbHelper.fetchAllCountries();

        // The desired columns to be bound
        String[] columns = new String[] { CountriesDbAdapter.KEY_CODE,
                CountriesDbAdapter.KEY_NAME, CountriesDbAdapter.KEY_CONTINENT,
                CountriesDbAdapter.KEY_REGION };

        // the XML defined views which the data will be bound to
        int[] to = new int[] { R.id.code, R.id.name, R.id.continent,
                R.id.region, };

        // create the adapter using the cursor pointing to the desired data
        // as well as the layout information
        dataAdapter = new SimpleCursorAdapter(this, R.layout.country_info,
                cursor, columns, to, 0);

SimpleCursorAdapter.ViewBinder binding = new SimpleCursorAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {


                TextView txtHeader = (TextView) findViewById(R.id.code);
        txtHeader.setTypeface(FontStyle
        .MonoSocialIconsFont(coursoradapter.this));


TextView txtHeader2 = (TextView) findViewById(R.id.name);
txtHeader2.setTypeface(FontStyle
        .MonoSocialIconsFont(coursoradapter.this));


TextView txtHeader4 = (TextView) findViewById(R.id.continent);
txtHeader4.setTypeface(FontStyle
        .MonoSocialIconsFont(coursoradapter.this));


TextView txtHeader3 = (TextView) findViewById(R.id.region);
txtHeader3.setTypeface(FontStyle
        .MonoSocialIconsFont(coursoradapter.this));


return false;
            };
};
dataAdapter.setViewBinder(binding);


        ListView listView = (ListView) findViewById(R.id.listView1);
        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);

This is the code for the class fontstyle.

package com.asolution.mathreeroute;

import android.content.Context;
import android.graphics.Typeface;

public class FontStyle {
    public static Typeface MonoSocialIconsFont(Context context) {
        Typeface tf = null;
        try {
            tf = Typeface.createFromAsset(context.getAssets(),
                    "MonoSocialIconsFont.ttf");
        } catch (Exception e) { e.printStackTrace(); } return tf; } }

The error in eclipse logcat is this.

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference
arthur kay
  • 116
  • 7

2 Answers2

0

Replace your all findViewById to view.findViewById in your setViewValue function.

EDITED instead of false return true in your setViewValue function. And in your setValue function, you have to put a switch case.

SimpleCursorAdapter.ViewBinder binding = new SimpleCursorAdapter.ViewBinder() {
    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

        switch (view.getId()) {
            case R.id.code:
            TextView txtHeader = ((TextView) view);
            txtHeader.setTypeface(FontStyle
                    .MonoSocialIconsFont(coursoradapter.this));
            break;

            case R.id.name:
            TextView txtHeader2 = ((TextView) view);
            txtHeader2.setTypeface(FontStyle
                    .MonoSocialIconsFont(coursoradapter.this));
            break;
            case R.id.continent;
            TextView txtHeader4 = ((TextView) view);
            txtHeader4.setTypeface(FontStyle
                    .MonoSocialIconsFont(coursoradapter.this));
            break;

            case R.id.region
            TextView txtHeader3 = ((TextView) view));
            txtHeader3.setTypeface(FontStyle
                    .MonoSocialIconsFont(coursoradapter.this));
        }
        return true;
    }
};
karandeep singh
  • 2,294
  • 1
  • 15
  • 22
  • Can you check whether the typeface you are returning is null or not? – karandeep singh Feb 11 '18 at 16:50
  • Same code for typeface works in about page in a simple text view. In debug mode I can also see the font variable has an id its only the textview that is null – arthur kay Feb 11 '18 at 17:10
  • In debug it shows that the find view variable is the one returning null. Same code is working in the about page for simple text box – arthur kay Feb 11 '18 at 17:15
  • Tried it but it shows only "text view" instead of data. Used abit of your code to target all text views ((TextView) view) – arthur kay Feb 11 '18 at 19:31
0

Ended up changing the code this way

    Typeface font =Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");
((TextView) view).setTypeface(font);

It partially works . The data fields are changed . But will have to work on the headers

arthur kay
  • 116
  • 7