1

我的活动中有两个片段。我正在实现 actionbarsherlock TabNavigation,每个选项卡都有单独的片段。我正在执行网络操作并将结果放在TextView.

如果我在当前片段完全加载后更改片段,它工作正常。只有当NullPointerException我在当前片段仍在加载时更改片段时才会发生。请帮忙。

片段 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;
}
}

片段 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;
}
}

提前致谢

4

1 回答 1

3

好吧,您已经回答了您自己的问题:NPE 发生是因为当前片段处于分离状态,这意味着它与活动分离并且没有指向它的链接 ->getActivity()返回null

您可以为您的检查添加几个额外的选项,以验证您的片段仍然可见并附加:

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);
        }
于 2013-06-03T08:00:52.517 回答