Starting Activity from Fragment causes NullPointerException
This is a known problem and I'm shocked to find that no one has filed a bug on the public bug tracker for this.
Your problem stems from this:
if (f.mSavedViewState != null) {
if (result == null) {
result = new Bundle();
}
result.putSparseParcelableArray(
FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
}
if (!f.mUserVisibleHint) {
// Only add this if it's not the default value
result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
}
You should notice that if result
is null
coming into this code and the first if
is not triggered you'll get a NullPointerException
if we enter the second if
.
It should read:
if (f.mSavedViewState != null) {
if (result == null) {
result = new Bundle();
}
result.putSparseParcelableArray(
FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState);
}
if (!f.mUserVisibleHint) {
if (result == null) {
result = new Bundle();
}
// Only add this if it's not the default value
result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint);
}
I submitted a patch for this a week or two ago: https://android-review.googlesource.com/31261
Starting Fragment activity from an activity causes NullPointerException
Your chipNavigationBar
variable is null and therefore the OnItemSelectedListener is instantiated with null, which is not possible.
You try to find your chipNavigationBar in the Dashboard, but you haven't even set setContentView(R.layout.yourLayout);
, which means that it is not possible to find the ViewById.
You have to instatiate the variable in your fragment, where you have a valid view.
chipNavigationBar = findViewById(R.id.bottom_nav_menu);
NullPointerException when calling an Activity method from a fragment, became a ClassCastException
private final TimerFragment timeFrag = (TimerFragment)getSupportFragmentManager().findFragmentByTag("TimerFragment");
You're calling this at the time your Activity
is created, which means your Fragment
wont exist. Move this code to the place where you need to use it:
void redirect(String app) {
final TimerFragment timeFrag = (TimerFragment)getSupportFragmentManager().findFragmentByTag("TimerFragment");
if(timeFrag != null) timeFrag.startTime(app);
}
Fragment - Unable to start activity ComponentInfo{}: java.lang.NullPointerException Android
You try to find ListView
which is define in Fragment
layout, onCreateView
method you inflate fragment layout into v (View)
so you find ListView
using v (View)
as below :
v.findViewById(R.id.lv_produitsList)
Instead of
getActivity().findViewById(R.id.lv_produitsList)
Using RunOnUIThread causing null pointer Exception After Switching to another Fragment
The first, you should understand about threads in Android. In Android, we have a thread called MainUIThread. I'm not sure that's the main thread or not. However, It has an instance of Looper
class.
Your fragment source basically runs on MainUIThread by default. When you create the sub-class of TimerTask
, you are creating a new Runnable for run your source in another thread, is that right?
Your error message java.lang.NullPointerException
means that the return value of getActivity()
method are null
. A good place to start is the JavaDocs. They have this covered:
Thrown when an application attempts to use null in a case where an object is required. These include:
- Calling the instance method of a null object.
- Accessing or modifying the field of a null object.
- Taking the length of null as if it were an array.
- Accessing or modifying the slots of null as if it were an array.
- Throwing null as if it were a Throwable value.
Applications should throw instances of this class to indicate other illegal uses of the null object.
The solution for you:
- Let check and make sure you are not finishing the Activity. (Another case that's you detach this Fragment of called Activity. Because you are using ViewPager, I guess that you are swipe two times to left or right. It automatically detach your fragment by default) Try to abandon it.
If you have to finish your Activity which contains your Fragment or has to detach your fragment. The easy way is checked null before call
runOnUiThread()
method.public class ViewPagerTask extends TimerTask {
@Override
public void run() {
Activity activity = getActivity();
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (viewPager.getCurrentItem() == 0) {
viewPager.setCurrentItem(1);
} else if (viewPager.getCurrentItem() == 1) {
viewPager.setCurrentItem(2);
} else {
viewPager.setCurrentItem(0);
}
}
});
}
}
}
Related Topics
Android: Scrollview VS Nestedscrollview
Menuitem Tinting on Appcompat Toolbar
How to Change Toolbar Home Icon Color
Highlighting Text Color Using HTML.Fromhtml() in Android
Android: How Get the Status-Code of an Httpclient Request
How to Generate 11 Char Hash Key for Sms Retriever with Google App Signing
Android-L Cardview Visual Touch Feedback
Handlers, Messagequeue, Looper, Do They All Run on the UI Thread
Cardview Not Showing Shadow in Android L
How to Achieve Overlap/Negative Margin on Constraint Layout
Android Show Notification with a Popup on Top of Any Application
How to Change Toolbar Navigation and Overflow Menu Icons (Appcompat V7)
How to Get a Jacoco Coverage Report Using Android Gradle Plugin 0.10.0 or Higher
React-Native, Android, Genymotion: Adb Server Didn't Ack