Home Activity Instance does not exist- null object reference
Reply: 0

Activity Instance does not exist- null object reference

user10484
1#
user10484 Published in September 21, 2018, 8:03 am

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.

share|improve this question
  • font/ikaros_light.otf files are in asset folder? – sasikumar Feb 12 at 7:20
  • in New Oreo version you can set the custom font in XML. – farhana Feb 12 at 7:27
  • where are you calling super() to inherit parent classes context? – paul_hundal Feb 12 at 7:30
  • 1
    If TextManager is not being used as an Activity, it should not extend an Activity. That's why the Context is null. Remove extends Start, and pass a Context to TextManager, either in a constructor, or a method. – Mike M. Feb 12 at 8:28
  • 1
    @MikeM this helped me out – Chinaedu Onwukwe Feb 12 at 21:44

3 Answers 3

active oldest votes
up vote 0 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.410457 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO