JozeRi
JozeRi

Reputation: 3429

Android - NullPointerException from creating an adapter

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?

Upvotes: 17

Views: 23120

Answers (5)

Amir Dora.
Amir Dora.

Reputation: 2717

Add checking for getContext()!=null

Before block

mAdapter = new SimpleHomeItemAdapter(getActivity(), mList);
listview.setAdapter(mAdapter);

For example:

if (getActivity()!=null){

mAdapter = new SimpleHomeItemAdapter(getActivity(), mList);
listview.setAdapter(mAdapter);

}

Upvotes: 0

JozeRi
JozeRi

Reputation: 3429

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 !

Upvotes: 44

Pedro Rom&#227;o
Pedro Rom&#227;o

Reputation: 2327

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);
    ...
}

Upvotes: 2

Srishti Roy
Srishti Roy

Reputation: 576

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);

Upvotes: 1

Shyantan deb
Shyantan deb

Reputation: 19

Use try catch block, it worked for me

Upvotes: -1

Related Questions