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