调用notifyDataSetChanged()会抛出异常
我有一个绑定到适配器的 ListView。该适配器是我的主类的受保护成员。我有一个侦听器线程,用于接收数据并更新适配器的源列表。当我调用适配器的notifyDataSetChanged()方法时,抛出异常:
运行时异常:PhoneLayoutInflater(LayoutInflater).inflate(int, ViewGroup,布尔值)行:322
我读到必须在 UI 线程上调用 notificationDataSetChanged() 方法,并且我正在这样做。事实上,为了确定,我什至在 UI 线程上更新了 ListView 的数据源。我在侦听器线程中所做的就是更新保存数据的受保护成员。这是我正在做的一个示例(请注意,我没有调用 AsyncTask 的代码部分,但它是从侦听器触发的):
public class main extends Activity() {
protected LinkedList<HashMap<String, String>> adapterDataList = new LinkedList<HashMap<String, String>>();
protected MyDataObject dataObject;
protected SimpleAdapter dataAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
//call parent oncreate
super.onCreate(savedInstanceState);
//display the main form
setContentView(R.layout.main);
//get a handle on the score list view elements
ListView dataList = (ListView)(findViewById(R.id.datalist));
//link the adapter and the data source
dataAdapter = new SimpleAdapter(this, adapterDataList, R.layout.row, new String[]{"name","address"}, new int[]{R.id.username,R.id.address});
dataList.setAdapter(dataAdapter);
}
protected void refreshData() {
adapterDataList.clear();
Iterator<MyData> iter = MyDataObject.iterator();
while(iter.hasNext()) {
HashMap<String, String> item = new HashMap<String, String>()
item.put("name", iter.name);
item.put("address", iter.address);
adapterDataList.add(item);
}
dataAdapter.notifyDataSetChanged();
}
private class DataTask extends AsyncTask<Data, Void, Data> {
protected void onPostExecute(Data data) {
dataObject = data;
runOnUiThread (new Runnable() { public void run() {
refreshScores();
}});
}
}
...
}
是否有人对我为什么得到这个有任何指示或想法例外?非常感谢任何帮助。
编辑 以下是根据要求的布局文件: main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ptl="http://schemas.android.com/apk/res/com.mod0.pubtrivialive"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/datalistcontainer">
<ListView android:id="@+id/datalist"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
行.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip">
<TextView android:id="@+id/name"
android:layout_width="80dip"
android:layout_height="wrap_content" />
<TextView android:id="@+id/address"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
I have a ListView that I'm binding to an adapter. The adapter is a protected member of my main class. I have a listener thread that receives data and updates the source list for my adapter. When I call the adapter's notifyDataSetChanged() method, an exception is thrown:
Runtime Exception: PhoneLayoutInflater(LayoutInflater).inflate(int,
ViewGroup, boolean) line: 322
I've read that the notifyDataSetChanged() method has to be called on the UI thread, and I'm doing that. In fact, just to be sure, I even update the ListView's data source on the UI thread. All I do in my listener thread is update a protected member that houses the data. Here's an example of what I'm doing (note that I don't have the part of the code that calls the AsyncTask, but it's fired from a listener):
public class main extends Activity() {
protected LinkedList<HashMap<String, String>> adapterDataList = new LinkedList<HashMap<String, String>>();
protected MyDataObject dataObject;
protected SimpleAdapter dataAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
//call parent oncreate
super.onCreate(savedInstanceState);
//display the main form
setContentView(R.layout.main);
//get a handle on the score list view elements
ListView dataList = (ListView)(findViewById(R.id.datalist));
//link the adapter and the data source
dataAdapter = new SimpleAdapter(this, adapterDataList, R.layout.row, new String[]{"name","address"}, new int[]{R.id.username,R.id.address});
dataList.setAdapter(dataAdapter);
}
protected void refreshData() {
adapterDataList.clear();
Iterator<MyData> iter = MyDataObject.iterator();
while(iter.hasNext()) {
HashMap<String, String> item = new HashMap<String, String>()
item.put("name", iter.name);
item.put("address", iter.address);
adapterDataList.add(item);
}
dataAdapter.notifyDataSetChanged();
}
private class DataTask extends AsyncTask<Data, Void, Data> {
protected void onPostExecute(Data data) {
dataObject = data;
runOnUiThread (new Runnable() { public void run() {
refreshScores();
}});
}
}
...
}
Does anyone have any pointers or ideas as to why I'm getting this exception? Any help is much appreciated.
edit
Here are the layout files, as requested:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ptl="http://schemas.android.com/apk/res/com.mod0.pubtrivialive"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/datalistcontainer">
<ListView android:id="@+id/datalist"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip">
<TextView android:id="@+id/name"
android:layout_width="80dip"
android:layout_height="wrap_content" />
<TextView android:id="@+id/address"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来不对
dataList.clear();
。您确定要在dataList
而不是adapterDataList
上调用 clear 吗?This doesn't look right
dataList.clear();
. Are you sure you're meaning to call clear ondataList
and notadapterDataList
?