调用notifyDataSetChanged()会抛出异常

发布于 2024-09-26 12:16:20 字数 3046 浏览 5 评论 0原文

我有一个绑定到适配器的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兔小萌 2024-10-03 12:16:20

这看起来不对 dataList.clear();。您确定要在 dataList 而不是 adapterDataList 上调用 clear 吗?

This doesn't look right dataList.clear();. Are you sure you're meaning to call clear on dataList and not adapterDataList?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文