【发布时间】:2014-08-28 17:02:23
【问题描述】:
我正在运行一个选项卡寻呼机适配器,但我似乎无法理解如何实现不在 Main Activity 中的代码。我制作的一些代码要求我扩展 Activity 使其成为不可能,因为我已经扩展了片段。有人能告诉我吗?
public class Home extends Fragment{
ListView list;
String url = "http://blog.compassion.com/living-in-the-philippines-rural-life/";
ProgressDialog mProgressDialog;
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
TextView titlebutton = (TextView) getActivity().findViewById(R.id.TextView01);
titlebutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
new Title().execute();
}
});
return inflater.inflate(R.layout.home_fragment, container);
}
private class Title extends AsyncTask<Void, Void, Void> {
String body;
String title;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setTitle("Android Basic JSoup Tutorial");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
Document document = Jsoup.connect(url).get();
Elements elements = document.select("p");
title = document.title();
for(Element elements123 : elements){
body+=elements123.text();
System.out.println(elements123.text());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
TextView txttbody = (TextView) getActivity().findViewById(R.id.textView2);
txttbody.setMovementMethod(new ScrollingMovementMethod());
txttbody.setText(title +"\n"+body);
mProgressDialog.dismiss();
}
}
【问题讨论】:
-
你有什么问题吗,你只是告诉我们你的心情。有什么问题?
-
我的代码不起作用。它在 onCreateView 部分有空指针。
标签: java android android-activity android-fragments android-asynctask