我整天都在研究这个问题,我准备把头发拔掉。我在这里和网上找到了一些答案,这些答案说这是由于尝试在线程内(而不是在 UI 线程中)对视图执行某些操作而引起的。但是我已经尝试了所有我见过的想法(处理程序/新线程),但仍然无法让它发挥作用。作为业余爱好,我用 C 编程了很多年,现在我是 Java/Android 的新手。我正在使用 Eclipse 和 Android 2.1 平台进行编程。我希望我的应用程序可以与尽可能多的 Android 手机一起使用,并且我认为我使用的所有功能都与 API 1 兼容。我还看到有一个叫做 AsyncTask 的东西,但这会导致问题有旧手机的人?
所以这就是我的应用程序所做的。当我单击一个按钮时,该应用程序会联机到一个网站并下载一个 xml/rss 提要。然后它解析它并使用我创建的自定义适配器将数据放入列表视图中。下载和解析可能需要 1 秒到 15 秒,所以我想添加一个进度对话框。添加之后,这就是我开始在这篇文章的标题中收到错误消息的地方。该应用程序成功下载(我在网络上的示例 xml 文件中有 8 条记录,因此它非常小),但是在显示列表视图之前我看到了错误。所以我想我需要确切地知道视图的哪一部分导致了错误,然后如何修复它。
这是代码(我已经删除了过去几个小时的所有测试代码,所以它很干净,不会让你们所有人感到困惑......和我):
@SuppressWarnings("serial")
public class ClubMessageList extends ListActivity implements Serializable
{
private static final String TAG = "DGMS News";
private ArrayList<CMessage> m_messages = null;
private MessageAdapter m_adapter;
private ProgressDialog m_ProgressDialog = null;
private Runnable downloadMessages;
// Need handler for callbacks to the UI thread
final Handler mHandler = new Handler();
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle icicle)
{
Log.i(TAG, "Starting the ClubMessageList activity");
super.onCreate(icicle);
setContentView(R.layout.list);
setTitle("DGMS News - Clubs");
try
{
// check to see if we already have downloaded messages available in the bundle
m_messages = (ArrayList<CMessage>) ((icicle == null) ? null : icicle.getSerializable("savedMessages"));
// if there are no messages in the bundle, download them from the web and then display them
if (m_messages == null)
{
m_messages = new ArrayList<CMessage>();
this.m_adapter = new MessageAdapter(this, R.layout.row_club, (ArrayList<CMessage>) m_messages);
setListAdapter(this.m_adapter);
downloadMessages = new Runnable(){
public void run() {
getMessages();
}
};
Thread thread = new Thread(null, downloadMessages, "DownloadMessages");
thread.start();
m_ProgressDialog = ProgressDialog.show(ClubMessageList.this,
"Please wait...", "Retrieving 2010 Show data ...", true);
}
else // messages were already downloaded, so display them in the listview (don't download them again)
{
Log.i("DGMS News", "Starting activity again. Data exists so don't retrieve it again.");
m_adapter = new MessageAdapter(this, R.layout.row_club, (ArrayList<CMessage>) m_messages);
this.setListAdapter(m_adapter);
}
}
catch (Throwable t)
{
Log.e("DGMS News",t.getMessage(),t);
}
}
private Runnable returnRes = new Runnable()
{
public void run()
{
if(m_messages != null && m_messages.size() > 0)
{
m_adapter.notifyDataSetChanged();
for(int i=0;i<m_messages.size();i++)
m_adapter.add(m_messages.get(i));
}
m_ProgressDialog.dismiss();
m_adapter.notifyDataSetChanged();
}
};
private void getMessages()
{
try
{
m_messages = new ArrayList<CMessage>();
ClubFeedParser parser = ClubFeedParserFactory.getParser();
m_messages = parser.parse();
for(int i = 0; i < m_messages.size(); i++)
m_adapter.add(m_messages.get(i));
}
catch (Exception e)
{
Log.e("DGMS News", e.getMessage());
}
runOnUiThread(returnRes);
}
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putSerializable("savedMessages", (Serializable) m_messages);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(ClubMessageList.this, ClubDetails.class);
// Add all info about the selected club to the intent
intent.putExtra("title", m_messages.get(position).getTitle());
intent.putExtra("location", m_messages.get(position).getLocation());
intent.putExtra("website", m_messages.get(position).getLink());
intent.putExtra("email", m_messages.get(position).getEmail());
intent.putExtra("city", m_messages.get(position).getCity());
intent.putExtra("contact", m_messages.get(position).getContact());
intent.putExtra("phone", m_messages.get(position).getPhone());
intent.putExtra("description", m_messages.get(position).getDescription());
startActivity(intent);
}
private class MessageAdapter extends ArrayAdapter<CMessage> implements Serializable
{
private ArrayList<CMessage> items;
public MessageAdapter(Context context, int textViewResourceId, ArrayList<CMessage> items)
{
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row_club, null);
CMessage m = items.get(position);
if (m != null)
{
TextView ltt = (TextView) v.findViewById(R.id.ltoptext);
TextView rtt = (TextView) v.findViewById(R.id.rtoptext);
TextView lbt = (TextView) v.findViewById(R.id.lbottext);
if (ltt != null)
ltt.setText(m.getTitle());
if (rtt != null)
rtt.setText(m.getLocation());
if (lbt != null)
lbt.setText(m.getCity() + ", CO");
//if (rbt != null)
; // not used in this list row
}
}
return v;
}
}
}
正如我所说,所有这些代码都运行良好,直到我添加了几天前在另一个网站上找到的进度对话框内容。
我感谢任何和所有的帮助,尽管我已经去 Android Developers 网站查看线程、处理程序等,这让我越来越困惑。实际的代码更改会很棒。:-) 今天看了这么多网站,我的头很疼。
谢谢!
鲍勃