java.lang.IllegalStateException?如何摆脱这个?

时间:2011-08-17 13:04:29

标签: android

我正在使用列表视图。滚动时我在其中更改内容或添加内容。它工作正常但如果我从该页面导航2或3次,则会出错。 错误是:

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131230910, class android.widget.ListView) with Adapter.


    @Override
    public View getView(final int position, View convertView, ViewGroup arg2)
    {
        Log.e("vec pos", ""+position);
        if(position == vectProductBySearch.size()-1 && scrollState == 0 && vectProductBySearch.size()>8)
        {
            Log.e("vec pos", "in if()");
            if (progDialog != null && progDialog.isShowing())
                progDialog.dismiss();
            progDialog = ProgressDialog.show(ProducyBySearch.this, "",
                    "Loading... Please wait..", true);

            new Thread(new Runnable() 
            {
                @Override
                public void run() 
                {
                    SearchByCatagory.startIndex= SearchByCatagory.startIndex+10;
                    post.getProductBySearch(AppsConstants.email, AppsConstants.password, AppsConstants.AuthenticationToken,  SearchByCatagory.CatagoryId,  SearchByCatagory.description,  SearchByCatagory.brand, SearchByCatagory.rating, AppsConstants.UserNum, SearchByCatagory.startIndex);
                    runOnUiThread(new Runnable() 
                    {
                        public void run() 
                        {
                            deafaultAdapter.refresh(AppsConstants.vectProductBySearch);
                            if (progDialog != null && progDialog.isShowing())
                                progDialog.dismiss();
                        }
                    });

                }
            }).start();

3 个答案:

答案 0 :(得分:0)

使用handler强制在UI线程中发生后台线程的任何更新。几乎所有环境/语言中的UI都不支持来自其他线程的更改

答案 1 :(得分:0)

确保使用UiThread修改列表内容,当列表内容发生变化时,不要忘记调用youradapter.notifyDataSetChanged()。

答案 2 :(得分:0)

在ui-thread之外,您可以使用post进行ui操作。 只需创建一个新的内联runnable-object并将代码添加到run() - 方法中。 它不是那么美丽,但在大多数情况下都有帮助。

示例:

view.post(new Runnable() {

        @Override
        public void run() {
            view.scrollTo(0, 0));

        }
    });