我的活动中有两个片段。我正在实现 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;
}
}
提前致谢