我正在连接到一个网站,我从那里获取新闻并将它们列入清单。当我点击新闻(标题)时,我想去另一个页面,在那里我可以看到新闻的内容。我正在使用jsoup这样做。 Logcat没有给我任何错误,但当我点击标题时,应用程序会崩溃。
这是我的活动:
公共类TargetActivity1扩展了Activity { ListView lista;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
Parse parsing = new Parse();
parsing.execute();
}
private class Parse extends AsyncTask<Void, Void, Void> {
String desc;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect(
"http://www.polimi.it/en/news/").get();
// Using Elements to get the Meta data
Elements description = document.select("div .news-list-item a");
// Locate the content attribute
desc = description.attr("href");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set description into TextView
WebView txtdesc = (WebView) findViewById(R.id.webView2);
txtdesc.addView(txtdesc);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(desc));
startActivity(i);
txtdesc.removeAllViews();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent myIntent = new Intent(getApplicationContext(),
MainActivity.class);
startActivityForResult(myIntent, 0);
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
你的Parse
课程错了,应该是这样的:
private class Parse extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String desc = "";
try {
// Connect to the web site
Document document = Jsoup.connect(
"http://www.polimi.it/en/news/").get();
// Using Elements to get the Meta data
Elements description = document.select("div .news-list-item a");
// Locate the content attribute
desc = description.attr("href");
} catch (IOException e) {
e.printStackTrace();
}
return desc;
}
@Override
protected void onPostExecute(String result) {
// Set description into TextView
WebView txtdesc = (WebView) findViewById(R.id.webView2);
txtdesc.addView(txtdesc); // adding txtdesc to txtdesc? it's wrong, bug
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(result));
startActivity(i);
txtdesc.removeAllViews();
}
}
请在此处阅读有关AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html的信息。如上所述,doInBackground
的结果可以传递给onPostExecute
。另请注意,您正在尝试为自己添加视图,我已对此进行了评论。