0

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.

Chinaedu Onwukwe
  • 437
  • 1
  • 7
  • 20

3 Answers3

1

Problem is context is null for getting assets.

Use getContext() or getApplicationContext() in case of being used in an activity but if it is being used in a fragment then use getActivity().getContext()

Typeface font = Typeface.createFromAsset(getContext().getAssets(),  "font/ikaros_light.otf");

Instead of

Typeface typeFaceIkarosLight= Typeface.createFromAsset(getAssets(), "font/ikaros_light.otf");
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
sasikumar
  • 12,540
  • 3
  • 28
  • 48
0

Its better to make method which return typeface instead passing textview ids as an argument. you can do it like this :

public Typeface getTypeFace(Context context){
    Typeface typeFaceIkarosLight = Typeface.createFromAsset(context.getAssets(), "font/ikaros_light.otf");
    return typeFaceIkarosLight;
}
Aj 27
  • 2,316
  • 21
  • 29
0

If you want to use custom fonts then you can take this example

This tutorial will show you how to set custom font on TextView, EditText and on Button.

http://androiderstack.com/index.php/2017/08/14/use-custom-font-in-textview-edittext-and-button-in-android/

This will surely help you

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40