I am creating an array adapter for a list view, everything works ok, I have 2 fragments, and 2 buttons at the top of the action bar that changes between this 2 fragments. my problem is that I get crashes if I move too fast between those frags, when I open fragOne, switch to fragTwo, and then quickly move back to fragOne.. fragOne throws a NPE from the getActivity context..
that's the line that crashes:
adapter = new MainFragmentDocumentAdapter(getActivity(), docsList, DocumentsFragment.this, page);
the log report :
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bbb.app, PID: 17438
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:104)
at com.bbb.app.UI.adapters.MainFragmentDocumentAdapter.<init>(MainFragmentDocumentAdapter.java:51)
any idea how can I solve this issue?
How to fix the NullPointerException? To avoid NullPointerException we have to initialize the Textview component with the help of findviewbyid( ) method as shown below. The findViewbyId( ) takes the “id” value of the component as the parameter. This method helps locate the component present in the app.
In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.
java.lang.NullPointerException. 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.
What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.
So basically after lots of check I found out that the problem was that I was returning to that fragment while inside a different fragment because I had a listener pointing out there and trying to open that method.
basically I just wrapped it in a
if (getActivity() != null) {
// Code goes here.
}
and problem solved.
thanks a lot though for all the help guys !
if you are on a Fragment check if the fragment is added.
{
...
if (!this.isAdded()) { //this = current fragment
return;
}
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
...
}
you can do like this.
inside activity
public static UsageRecommendationTabActivity getInstance() {
return activityInstance;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityInstance=this;
}
inside fragment
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
if (activity instanceof UsageRecommendationTabActivity)
mParentActivity = (UsageRecommendationTabActivity) activity;
if (mParentActivity == null)
mParentActivity = UsageRecommendationTabActivity.getInstance();
}
and then call your adapter
adapter = new MainFragmentDocumentAdapter(mParentActivity, docsList, DocumentsFragment.this, page);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With