I am trying to develop an Android application. For my use case I want to use a custom typeface and I wrote a that gathers all available TextViews
in a View, so that I can set the typeface easily by a loop. I thought I should source out the text manipulation things to an own class named TextManager.class
. But now when I am executing the app I am getting an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
It occurs when I am trying to set Typeface in TextMangaer.class . I did a bit of research and found out that it is because the activity instance does not exist at this point. But I don't get it why, cause when I am trying to do this in Start.class there is no problem.
//Start.class
public class Start extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // set fullscreen
//Initialize shared preferences
prefs = getSharedPreferences("User", Context.MODE_PRIVATE);
editor=prefs.edit();
setContentView(R.layout.start_screen);
TextManager textManager= new TextManager();
textManager.setTypeface(getTextViews((ViewGroup) findViewById(R.id.root_menu)));
}
}
and my TextManager.class:
public class TextManager extends Start{
public TextManager(){
super();
}
public void setTypeface(List<Integer> idsOfTextViews){
Typeface typeFaceIkarosLight= Typeface.createFromAsset(getAssets(), "font/ikaros_light.otf");
for(int i=0; i < idsOfTextViews.size();i++){
((TextView)findViewById(idsOfTextViews.get(i))).setTypeface(typeFaceIkarosLight);
}
}
}
So how could I fix this or how should I write this? If anybody could help me figure it out that would be great. Thanks in advance.