gpt4 book ai didi

android - 使用 AsyncTask 和进程栏

转载 作者:行者123 更新时间:2023-11-30 04:17:36 24 4
gpt4 key购买 nike

我编写了一些应用程序,使用 lauot-procce bar 和 AsyncTask 来处理 xml 并将其显示给用户。我想在后处理完成后取消处理栏但是我的代码有问题

错误:

FATAL EXCEPTION: AsyncTask #2
03-17 11:25:29.559: E/AndroidRuntime(31114): java.lang.RuntimeException: An error while executing doInBackground()
03-17 11:25:29.559: E/AndroidRuntime(31114): at android.os.AsyncTask$3.done(AsyncTask.java:200)
03-17 11:25:29.559: E/AndroidRuntime(31114): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
03-17 11:25:29.559: E/AndroidRuntime(31114): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
03-17 11:25:29.559: E/AndroidRuntime(31114): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
03-17 11:25:29.559: E/AndroidRuntime(31114): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
03-17 11:25:29.559: E/AndroidRuntime(31114): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
03-17 11:25:29.559: E/AndroidRuntime(31114): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
03-17 11:25:29.559: E/AndroidRuntime(31114): at java.lang.Thread.run(Thread.java:1019)
03-17 11:25:29.559: E/AndroidRuntime(31114): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-17 11:25:29.559: E/AndroidRuntime(31114): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
0 3-17 11:25:29.559: E/AndroidRuntime(31114): at java.util.ArrayList.get(ArrayList.java:311)
03-17 11:25:29.559: E/AndroidRuntime(31114): at com.example.News.NewsActivity$GetDataTask.doInBackground(NewsActivity.java:67)
03-17 11:25:29.559: E/AndroidRuntime(31114): at

我的代码是:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progdailog);
mContext = this;
new GetDataTask().execute();
}
protected void onPostExecute(Integer result) {

listAdapter = new CustomListAdapter(mContext, R.layout.list_item,
ListNews);
ListView lv = ((ListActivity) mContext).getListView();
lv.setAdapter(listAdapter); // on test

lv.setTextFilterEnabled(true);

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position != 0) {

// When clicked, show a toast with the TextView text
String s = ReadXML.hadashotListItems.get(position - 1).link;

Intent intent = new Intent(NewsActivity.this,
WebActivity.class);
intent.putExtra("url", s);

startActivity(intent);

}
}

public void onScrollStateChanged(final AbsListView view,
final int scrollState) {
if (scrollState == 0)
listAdapter.notifyDataSetChanged();
}
});

//progDailog.dismiss();
super.onPostExecute(result);

}

}

第 67 行在代码中:

 protected Integer doInBackground(Void... params) {
try {
ReadXML = new ReadXMLFile(mContext, getIntent().getExtras()
.getString("urlXml"));

ListNews.add("NEWS");
if (checkDate(ReadXML.hadashotListItems.get(0).pubDate,ReadXML.hadashotListItems.get(1).pubDate))
for (int i = 0; i < ReadXML.hadashotListItems.size(); i++) {

ListNews.add(ReadXML.hadashotListItems.get(i).title + "\n"
+ ReadXML.hadashotListItems.get(i).pubDate);
}
else
for (int i = ReadXML.hadashotListItems.size()-1; i >=0; i--) {

ListNews.add(ReadXML.hadashotListItems.get(i).title + "\n"
+ ReadXML.hadashotListItems.get(i).pubDate);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return 1;
}

我认为 View 有问题。希望您能帮助更改可以正常工作的代码。

最佳答案

NewsActivity.java 的第 67 行有一个 IndexOutOfBoundsException。

IndexOutOfBoundsException: Invalid index 0, size is 0

您试图在没有元素时访问索引 0。我认为您没有发布该代码,因此查找该行并修复不正确的索引访问。

关于android - 使用 AsyncTask 和进程栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9748942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com