我正在尝试构建一个基本的 RSS 提要。我在代码中没有显示任何错误,但是当我运行应用程序时,它会因以下错误而崩溃:
致命异常:AsyncTask #1 java.lang.RuntimeException:执行 doInBackground() 时发生错误
这是我的 MainActivity.java 代码:
public class MainActivity extends ListActivity {
List<String> headlines;
List<String>links;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize variables
headlines = new ArrayList<String>();
links = new ArrayList<String>();
new PostTask().execute();
//binding data to list
ArrayAdapter <String>adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, headlines);
setListAdapter(adapter);
}
private InputStream getInputStream(URL url) {
try{
return url.openConnection().getInputStream();
}catch(IOException e){
return null;
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Uri uri = Uri.parse(links.get(position));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
//ASYNCH CLASS
private class PostTask extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... arg0) {
try{
//link to data source
URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");
//Set up parser
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
//get XML from input stream
xpp.setInput(getInputStream(url), "UTF_8");
//Keep track of which tag inside of XML
boolean insideItem = false;
//Loop through the XML file and extract data required
int eventType = xpp.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT){
if(xpp.getName().equalsIgnoreCase("item")){
insideItem = true;
}else if(xpp.getName().equalsIgnoreCase("title")){
if(insideItem){
//Get title
headlines.add(xpp.nextText());
}
}else if(xpp.getName().equalsIgnoreCase("link")){
if(insideItem){
//Get link
links.add(xpp.nextText());
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem = false;
}
eventType = xpp.next();//move to next element
}
}catch(MalformedURLException e){
e.printStackTrace();
}catch(XmlPullParserException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return null;
}
public void onPostExecute(URL url) {
}
}
}
谁能指出我哪里出错了?非常感谢!
记录猫错误:
01-11 17:27:50.061: E/AndroidRuntime(7746): FATAL EXCEPTION: AsyncTask #1
01-11 17:27:50.061: E/AndroidRuntime(7746): java.lang.RuntimeException: An error occured while executing doInBackground()
01-11 17:27:50.061: E/AndroidRuntime(7746): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-11 17:27:50.061: E/AndroidRuntime(7746): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-11 17:27:50.061: E/AndroidRuntime(7746): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-11 17:27:50.061: E/AndroidRuntime(7746): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-11 17:27:50.061: E/AndroidRuntime(7746): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-11 17:27:50.061: E/AndroidRuntime(7746): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-11 17:27:50.061: E/AndroidRuntime(7746): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-11 17:27:50.061: E/AndroidRuntime(7746): at java.lang.Thread.run(Thread.java:856)
01-11 17:27:50.061: E/AndroidRuntime(7746): Caused by: java.lang.NullPointerException
01-11 17:27:50.061: E/AndroidRuntime(7746): at com.example.simplerss.MainActivity$PostTask.doInBackground(MainActivity.java:89)
01-11 17:27:50.061: E/AndroidRuntime(7746): at com.example.simplerss.MainActivity$PostTask.doInBackground(MainActivity.java:1)
01-11 17:27:50.061: E/AndroidRuntime(7746): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-11 17:27:50.061: E/AndroidRuntime(7746): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-11 17:27:50.061: E/AndroidRuntime(7746): ... 4 more