Skip to content Skip to sidebar Skip to footer

Activity Is Null When Passing It To My Custom Adapter Class

Acvitity is null when i am passing to my Custom Adapter. This happens when i change the orientation either from portrait to landscape or vice-versa. I debugged, but could not figur

Solution 1:

When a fragment is detached from an activity, it sets its activity reference to null. So you should either save a reference to the activity in fragment's onAttach() method:

privateSherlockActivity mActivity;

@OverridepublicvoidonAttach(Activity activity) {
    super.onAttach(activity);
    mActivity = getSherlockActivity();
}

@OverridepublicvoidonLoadComplete(List<DocumentResponse> data) {
    DocumentsAdapter adapter =
            newDocumentsAdapter(mActivity, android.R.id.list, data);
    setListAdapter(adapter);
}

or check if the activity is not null before actually accessing it:

@OverridepublicvoidonLoadComplete(List<DocumentResponse> data) {
    SherlockActivityactivity= getSherlockActivity();
    if (activity != null) {
        DocumentsAdapteradapter=newDocumentsAdapter(activity, android.R.id.list, data);
        setListAdapter(adapter);
    }
}

Post a Comment for "Activity Is Null When Passing It To My Custom Adapter Class"