I wrote a sample application for the NavigationDrawer pattern with the ActionBar-Compat library project. Every time i change from portrait to landscape mode i got a NullPointerException during onAttach(). The Activity returns null for the getSupportActionBar() method. This happens in the Fragment which is changed by the NavigationDrawer. You can find the code on my github project: FadingActionBar-Compat (Line 162)
Maybe it is a error which can be fixed by a update from google in the future?
I found the solution by looking in to the ActionBar Compat source code. The ActionBar is ready to use in the lifecycle methode onActivityCreated(). You musst call the super-Method before. After that you can call getSupportActionBar() without a NullPointerException.
Related
actionBar = getActionBar(); is working fine In most of the new mobile sets, but in some old set it give error -
"application stopped unexpectedly"
Can somebody please tell me why it is so, or is there any alternatives for getActionBar(), like actionbar = new ActionBar(). I am not using support.v7
Start using v7 appcompat library, as it is the only way.
Read this : https://developer.android.com/topic/libraries/support-library/features.html#v7
Add this line in the build.gradle and re-sync:
compile 'com.android.support:appcompat-v7:23.2.0'.
Refractor your activity classes by extending to AppCompactActivity instead of Activity.
If you've set custom action bar, call setSupportActionBar([your_custom_toolbar]) in OnCreate() method.
You can now call getSupportActionBar() error-free :)
in your onCreate() function use this line to set your ActionBar.
getSupportActionBar().show();
Hope it solves the problem!
Older devices that you are using don't have the ActionBar; so the application crashes when you try to access it.
So, to support ActionBar on older devices, you can use the support library and use getSupportActionBar() to access it.
Instead of using the plain vanilla Activity, you should be using ActionBarActivity to be able to use the getSupportActionBar() method.
I have the same issue has this post: android.support.v4.app.Fragment: undefined method getChildFragmentManager(). I need to use getChildFragmentManager() because I'm using nested fragments.
My problem is: the solution in the other post does not work for me:
My SDK is updated.
I got my android-support-v4 from the SDK folder.
I added android-support-v4 both the my project and ABS.
Here is my libs:
How can I correctly use this method?
Not sure if you've resolved your issue yet, but I was having the same issue and I resolved it. The getCildFragmentManager() method is a method of the class Fragment and not FragmentActivity. Just because your class extends fragment activity does not mean you'll have access to this method. What I did is I created an instance of my fragment in which I wanted to place the child fragment and invoked the method through that fragment instance. There are obviously other approaches to this, but this worked for me.
maybe something in your android manifest, look at the header or 'footer of your android manifest about minimum SDK version.
This is because by using old android.support.v4.
once you change the latest library.it will solved
I solved it by change it.
Hope it will work
I'm look for the easiest way to get an ActionBar instance from a Fragment using AppCompatLibrary and API 8.
Already tried things like
getActivity().getSupportActionBar()
but no luck.
Try to cast it:
((YourActivity)getActivity()).getSupportActionBar()
I have an Android app with TabManager. Due to change of Android API, I need to upgrade my app for Activity to contain the Fragment. To provide backward compatibility, I use ActionBarSherlock.
My App is working fine. However, looking at the Google Play Developer Console, there is always few crash reports on "java.lang.NullPointerException" on the line with getSherlockActivity(), I think only less than 0.1% out of total users are affected.
For example,
// Example 1
File file = new File(getSherlockActivity().getCacheDir(), "filename");
// Example 2
getSherlockActivity().setSupportProgressBarIndeterminateVisibility(false);
My question:
1. Should I change all the getSherlockActivity() to getActivity()? Or under certain rule, it is mandatory to use one of them?
2. What is the difference between them?
Thanks a lot.
The only difference is that with getSherlockActivity () you get the result of getActivity() but casted as a SherlockActivity. This allows to get access to the ABS specific apis.
If you just need something that is general enough to be in the Activity class, just use getActivity(). It will highlight this, otherwise highlight the fact that you use something that is ABS specific using getSherlockActivity ().
The NPE can come from :
using getActivity() (or siblings...) before onAttach has been executed
using getActivity() (or siblings...) after onDetach has been executed
So the solution is to check if your fragment is attached before using its activity :
if( isAttached() ) {
getActivity()....
}
Same topic
The NullPointerException is normally
Caution: If you need a Context object within your Fragment, you can call getActivity(). However, be careful to call getActivity() only when the fragment is attached to an activity. When the fragment is not yet attached, or was detached during the end of its lifecycle, getActivity() will return null.
This exception probably means that the code is being exectuted when the fragment is not attached to an activity.
The reference returned by getSherlockActivity() is set when the Fragment is attached, and then cleared (set to null) when the Fragment is detached. If your code tries to reference getSherlockActivity() before or after this, you would get a null-pointer.
getSherlockActivity is just a shortcut to:
(SherlockActivity) getActivity()
So in your case there is no problem to use getActivity() instead in Example 1 but in Example 2 it will not work since it is a method of SherlockActivity.
So basically I have updated my application from using just activities in a tabBar to using SherlockFragments in a supportActionBar. Not a big deal you would think, and so did I. Everything worked perfectly through all of my tests. However when I released the new version my users started complaining and reporting numerous crashes where fragments throw a NPE when calling getSherlockActivity().
I have read about this problem and it's a problem with restoring instance states and fragments that are no longer attached to any activity but still run for some reason. I haven't found a specific solution for my problem (since I'm using the actionBar to display my tabs instead of a ViewPager).
Now my question is if I can simply discard these fragments that return null when calling getSherlockActivity()? Because they aren't attached to any activity, are they also not visible?
If not, how could I solve this problem? I am already checking the FragmentManager for already existing fragments before creating them, but this isn't sufficient.
Please help me!
You don't need to discard anything. All necessary checks are needed to be done in a Fragment.
You should try to avoid using getSherlockActivity() before onAttach() is called (since it is called before onCreate(), should be no problems with this, but still) and after onDetach() is called.
if (isAdded() && !isDetached())
or
final SherlockActivity a = getSherlockActivity();
if (a != null)
Might work okay.