【问题标题】:Strange IllegalStateException with ViewPager and TextView带有 ViewPager 和 TextView 的奇怪 IllegalStateException
【发布时间】:2014-10-02 03:26:39
【问题描述】:

我的应用发生了奇怪的崩溃。

在我的布局上,我有一个独立于我的 viewPager 的 TextView,它具有 android:layout_below 属性,因此,TextView 的父级是一个具有 ID rl 的 RelativeLayout,而 ViewPager 也是:

现在,当我更改 ViewPager 的适配器时,我同时更改了 TextView 的文本,我得到以下IllegalStateException

    ChiamataPOST chiamata = new ChiamataPOST(WSEntity); // My AsyncTask
    chiamata.execute(WS); // I execute it

    cittaSceltaTV.setText("whatever text"); // and I change my textView, I've tried to do it before and after the AsyncTask.

java.lang.IllegalStateException:应用程序的 PagerAdapter 更改了适配器的内容,而没有调用 PagerAdapter#notifyDataSetChanged!预期的适配器项目数:4,找到:0

但如果我评论 TextView 的 setText() 的功能,它会完美运行。

这很奇怪吗?我错了吗?

提前致谢。

【问题讨论】:

    标签: android android-viewpager textview illegalstateexception android-pageradapter


    【解决方案1】:

    在您的文本视图上调用 setText() 之前,先在 PageAdapter 上调用 notifyDataSetChanged()。如下所示

    notifyDataSetChanged();
    cittaSceltaTV.setText("whatever text"); 
    

    我相信,一旦您调用 setText(),它就会尝试更新列表并抛出 IllegalStateException,因为它注意到预期项目数与实际项目数之间存在差异。见下文。

     Expected adapter item count: 4, found: 0
    

    【讨论】:

    • 谢谢...每次我更换适配器时我都会调用 notifyDataSetChanged 但那个...但我仍然不明白为什么会发生这种情况。非常感谢!
    猜你喜欢
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多