本文介绍了执行" doInBackground()"显示抛出:IllegalArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序下载量从新闻网页的标题,并在列表视图中显示它们,还挺RSSReader,现在当我尝试运行它,但它会显示错误。
My app downloads titles from the news webpage and display them in a listview, kinda a RSSReader, now when i try to run it but it shows error.
code: -
public class RSSReaderActivity extends ListActivity
{ List headlines, links;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new DoSomeTask().execute();
}
// DoSomeTask class will do all the work on another thread so there will be no
// ANR and UI hanging.
private class DoSomeTask extends AsyncTask<Void, Void, Void>
{
/* (non-Javadoc)
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected Void doInBackground(Void... arg0) {
try {
headlines = new ArrayList();
links = new ArrayList();
URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if (eventType == XmlPullParser.START_TAG)
{
if (xpp.getName().equalsIgnoreCase("item"))
{
insideItem = true;
}
else if (xpp.getName().equalsIgnoreCase("title"))
{
if (insideItem)
headlines.add(xpp.nextText()); //extract the headline
}
else if (xpp.getName().equalsIgnoreCase("link"))
{
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
}
}
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;
}
protected void onPostExecute(Void params)
{
try{
// Binding data
ArrayAdapter adapter = new ArrayAdapter(RSSReaderActivity.this,
android.R.layout.simple_list_item_1, headlines);
setListAdapter(adapter);
}catch (Exception e) {}
}
}
public InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
protected void onListItemClick(ListView l, View v, int position, long id) {
Uri uri = Uri.parse((String) links.get(position));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
的logcat: -
06-23 19:37:57.505: W/dalvikvm(7450): threadid=8: thread exiting with uncaught exception (group=0x400207d8)
06-23 19:37:57.785: E/AndroidRuntime(7450): FATAL EXCEPTION: AsyncTask #1
06-23 19:37:57.785: E/AndroidRuntime(7450): java.lang.RuntimeException: An error occured while executing doInBackground()
06-23 19:37:57.785: E/AndroidRuntime(7450): at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.lang.Thread.run(Thread.java:1096)
06-23 19:37:57.785: E/AndroidRuntime(7450): Caused by: java.lang.IllegalArgumentException
06-23 19:37:57.785: E/AndroidRuntime(7450): at org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1040)
06-23 19:37:57.785: E/AndroidRuntime(7450): at mohit.apps.rssreaderfast.RSSReaderActivity$DoSomeTask.doInBackground(RSSReaderActivity.java:56)
06-23 19:37:57.785: E/AndroidRuntime(7450): at mohit.apps.rssreaderfast.RSSReaderActivity$DoSomeTask.doInBackground(RSSReaderActivity.java:1)
06-23 19:37:57.785: E/AndroidRuntime(7450): at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-23 19:37:57.785: E/AndroidRuntime(7450): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-23 19:37:57.785: E/AndroidRuntime(7450): ... 4 more
所以,是的,误差在doInBackground(),因为我是新的多线程编程,请点我出什么与code中的问题。谢谢你。
So yeah, the error is in doInBackground(), as i am new multi-thread programming, please point me out whats the problem with the code. Thanks.
推荐答案
呼... atlast其固定。
Phew...atlast its fixed.
我刚换
xpp.setInput(getInputStream(url), "UTF_8");
到
xpp.setInput(url.openConnection().getInputStream(), "UTF_8");
和中提琴:)
这篇关于执行&QUOT; doInBackground()&QUOT;显示抛出:IllegalArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!