Answer the question
In order to leave comments, you need to log in
How to get around NullPointerException when working with ActionBar?
Applications are developed for APIs >= 15.
As activities are used by AppCompatActivity, fragments are common.
When working with the ActionBar (whether it's setting text, a back button, icons, etc.), a warning about a possible NullPointerException always pops up, and when the application starts, it crashes.
Neither getActionBar() nor getSupportActionBar() works, a warning pops up in both cases.
I went around a bunch of questions on the topic and a normal solution was not found. There were all sorts of debug versions where, using the toolbar and setSupportActionBar(), something came out, but in the end I still had to catch NullPointerException, which is not at all happy.
Even just to add an icon, I had to create separate styles for the ActionBar, which is in no way convenient, given the existence of special methods for this.
Perhaps someone knows a working formula to solve the problem?
Answer the question
In order to leave comments, you need to log in
Most likely in your styles.xml file your main theme looks something like this:
The key point is NoActionBar. Here you need to use the Toolbar, place it yourself in the layouts, and call the setSupportActionBar(toolbar) method in the Activity
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question