Lalith Mohan
Lalith Mohan

Reputation: 3906

Android: NullPointerException when changing fragment

I have two fragments in my activity. I'm implementing actionbarsherlock TabNavigation and each tab has separate fragment. I'm performing network operation and place the results in a TextView.

If I change the fragment after the current fragment is completely loaded, it works fine. The NullPointerException only occurs if I change the fragment while the current fragment is still loading. Please help.

FRAGMENT 1:

public class Fragment1 extends SherlockFragment {

public static Fragment1 newInstance() {
    Fragment1 f = new Fragment1 ();
    return f;
}

private static LinearLayout mainll;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub4

    final View V = inflater.inflate(R.layout.fragment_1, container, false);

    AsyncHttpClient client = new AsyncHttpClient();
    client.get(((MainActivity) act).getUrl(), new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            Document document = Jsoup.parse(response);

            Element results = document.getElementById("search_results");

            mainll = (LinearLayout) V.findViewById(R.id.content);

            LayoutParams mainllParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            mainllParams.setMargins(10, 5, 0, 0);

            if(results != null) {
                TextView tv= new TextView(getActivity());  //NPE occurs here
                tv.setLayoutParams(mainllParams);
                tv.setText(results.ownText());
                tv.setGravity(Gravity.CENTER_HORIZONTAL);
                mainll.addView(tv);
            }
    });

    return V;
}
}

FRAGMENT 2:

public class Fragment2 extends SherlockFragment {

public static Fragment2 newInstance() {
    Fragment2 f = new Fragment2 ();
    return f;
}

private static LinearLayout mainll;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub4

    final View V = inflater.inflate(R.layout.fragment_1, container, false);

    AsyncHttpClient client = new AsyncHttpClient();
    client.get(((MainActivity) act).getUrl(), new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            Document document = Jsoup.parse(response);

            Element results2 = document.getElementById("more_results");

            mainll = (LinearLayout) V.findViewById(R.id.content);

            LayoutParams mainllParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            mainllParams.setMargins(10, 5, 0, 0);

            if(results2 != null) {
                TextView tv2 = new TextView(getActivity());  //NPE Occurs here
                tv2.setLayoutParams(mainllParams);
                tv2.setText(results.ownText());
                tv2.setGravity(Gravity.CENTER_HORIZONTAL);
                mainll.addView(tv2);
            }
    });

    return V;
}
}

Thanks in advance

Upvotes: 1

Views: 908

Answers (1)

Evos
Evos

Reputation: 3915

Well, you already answered your own question: NPE occurs because current fragment is in detached state, it's mean it detached from activity and have no link to it -> getActivity() returns null.

You could add several extra options for your check to verify that your fragment is still visible and attached:

if(results2 != null && getActivity()!=null && isVisible()) {
            TextView tv2 = new TextView(getActivity());  //NPE Occurs here
            tv2.setLayoutParams(mainllParams);
            tv2.setText(results.ownText());
            tv2.setGravity(Gravity.CENTER_HORIZONTAL);
            mainll.addView(tv2);
        }

Upvotes: 3

Related Questions