I have a MainActivity
class that has a method called: updateTabTitles()
;
updateTabTitles()
does what it sounds like, updates the titles of my tabs.
String tabName = tabNames[tabNumber];
String font = "helveticaneuebold.ttf"; // Fonts found in assets/fonts/ folder
SpannableString title = new SpannableString(tabName);
SpannableString subTitle;
// This line gives me trouble
title.setSpan(new TypefaceSpan(getBaseContext(), font), 0, title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
I call this function from my ChecklistTab
class that extends the Fragment
class. The following method is in my ChecklistTab
class:
public int calculateRemaining(ArrayList<ChecklistItem> checklistItems, int tabNumber) {
int remaining = 0;
for (int i = 0; i < checklistItems.size(); i++) {
if (!checklistItems.get(i).getCheckBox().isChecked() && checklistItems.get(i).getCheckBox().isEnabled() && !checklistItems.get(i).getCheckBox().getText().equals("N/A")) {
remaining++;
}
}
// NULL POINTER HERE
((MainActivity) getActivity()).updateTabTitles(tabNumber, remaining);
return remaining;
}
Lastly, I found a custom TypefaceSpan
class online for changing fonts. This is the constructor that is called from updateTabTitles()
public TypefaceSpan(Context context, String typefaceName) {
mTypeface = sTypefaceCache.get(typefaceName);
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getApplicationContext()
.getAssets(), String.format("fonts/%s", typefaceName));
// Cache the loaded Typeface
sTypefaceCache.put(typefaceName, mTypeface);
}
}
I really don't know much about contexts, but this code used to work with my old implementation. However, since refactoring, I can't seem to figure out what's wrong now.
Stacktrace:
java.lang.NullPointerException
at com.brettrosen.atls.fragments.ChecklistTab.calculateRemaining(ChecklistTab.java:179)
at com.brettrosen.atls.onclicklisteners.CheckboxOnClickListener.onClick(CheckboxOnClickListener.java:46)
at android.view.View.performClick(View.java:4192)
at android.widget.CompoundButton.performClick(CompoundButton.java:100)
at android.view.View$PerformClick.run(View.java:17327)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5019)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)