Android:AsyncTask 强制关闭
我有一个 asynctask 可以解析来自网络的 xml 文件。我将其值存储到主要活动的变量中。当我运行以下代码时,它强制关闭。
private class parseXMLAsync extends AsyncTask <String, String, String>{
protected void onPreExecute(){
super.onPreExecute();
showDialog(PARSE_XML);
}
@Override
protected String doInBackground(String... strings) {
try{
Engagia.this.url.openConnection();
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);
xr.parse(new InputSource(Engagia.this.url.openStream()));
List<ParsedExampleDataSet> parsedExampleDataSet = myExampleHandler.getParsedData();
Iterator i;
i = parsedExampleDataSet.iterator();
ParsedExampleDataSet dataItem;
while(i.hasNext()){
dataItem = (ParsedExampleDataSet) i.next();
String folder = dataItem.getParentTag();
if( folder == "Videos" ){
MainAct.this.videoNames[MainAct.this.videoCount] = dataItem.getName();
MainAct.this.videoCount++;
}
}
}catch(Exception e){
Log.e(LOG_TAG, e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String lenghtOfFile) {
if( mProgressDialog.isShowing() ){
dismissDialog(PARSE_XML);
}
String str_contents = null;
PopIt("Parsing Done", "STR CONTENTS >> " + str_contents, "Denied");
}
}
logcat 说:
I have an asynctask that parses an xml file from the net. I'm storing its value to a variable of the main activity. When I run the following code, it force closes.
private class parseXMLAsync extends AsyncTask <String, String, String>{
protected void onPreExecute(){
super.onPreExecute();
showDialog(PARSE_XML);
}
@Override
protected String doInBackground(String... strings) {
try{
Engagia.this.url.openConnection();
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);
xr.parse(new InputSource(Engagia.this.url.openStream()));
List<ParsedExampleDataSet> parsedExampleDataSet = myExampleHandler.getParsedData();
Iterator i;
i = parsedExampleDataSet.iterator();
ParsedExampleDataSet dataItem;
while(i.hasNext()){
dataItem = (ParsedExampleDataSet) i.next();
String folder = dataItem.getParentTag();
if( folder == "Videos" ){
MainAct.this.videoNames[MainAct.this.videoCount] = dataItem.getName();
MainAct.this.videoCount++;
}
}
}catch(Exception e){
Log.e(LOG_TAG, e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String lenghtOfFile) {
if( mProgressDialog.isShowing() ){
dismissDialog(PARSE_XML);
}
String str_contents = null;
PopIt("Parsing Done", "STR CONTENTS >> " + str_contents, "Denied");
}
}
The logcat says:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的异常似乎没有消息。所以它向记录器发送 null,而记录器不喜欢它。您可以改为发送 e.toString()。
Your exception does not seem to have a message. So it sends null to logger and logger does not like it. You can send e.toString() instead.