我正在尝试构建一个基本的RSS Feed。我没有在代码中显示错误,但是当我运行应用程序时,它崩溃并出现以下错误:
致命异常: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) {
}
}
}
有人能指出我哪里出错了吗?非常感谢!
登录CAT错误:
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
答案 0 :(得分:1)
我想当xpp.getClass()
引用一组标记内的文本时xpp
返回null。如果您查看XmlPullParser's documentation中的示例:
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == XmlPullParser.START_TAG) {
System.out.println("Start tag "+xpp.getName());
} else if(eventType == XmlPullParser.END_TAG) {
System.out.println("End tag "+xpp.getName());
} else if(eventType == XmlPullParser.TEXT) {
System.out.println("Text "+xpp.getText());
}
eventType = xpp.next();
}
请注意,重点是eventType
。如果你只想抓住"title"
和
"link"
代码,在START_TAG
:
else if(eventType == XmlPullParser.START_TAG) {
if(xpp.getName().equalsIgnoreCase("title")){
//Get title
}else if(xpp.getName().equalsIgnoreCase("link")){
//Get link
}
}