【问题标题】:Android: ClassCastException when adding a header view to ExpandableListViewAndroid:将标题视图添加到 ExpandableListView 时出现 ClassCastException
【发布时间】:2011-05-22 13:57:40
【问题描述】:

我正在尝试向 ExpandableListView 添加标题,如下所示:

headerView = View.inflate(this, R.layout.header, null);
expandableListView.addHeaderView(headerView);
expandableListView.setAdapter(new SectionedAdapter(this));

这给了我以下错误:

 12-08 16:23:42.354:
 ERROR/AndroidRuntime(421): Caused by:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
 12-08 16:23:42.354:   ERROR/AndroidRuntime(421): at android.widget.ListView.clearRecycledState(ListView.java:504)
 12-08 16:23:42.354: ERROR/AndroidRuntime(421): at android.widget.ListView.resetList(ListView.java:490)
 12-08 16:23:42.354:ERROR/AndroidRuntime(421):at android.widget.ListView.setAdapter(ListView.java:422)
 12-08 16:23:42.354:ERROR/AndroidRuntime(421): at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:475)

这发生在拨打expandableListView.setAdapter(new SectionedAdapter(this)) 时,但我不知道为什么。有什么想法吗?

【问题讨论】:

    标签: android android-linearlayout expandablelistview classcastexception


    【解决方案1】:

    好的,我想通了。通过以编程方式将 View 的 LayoutParams 设置为 ListView LayoutParams,我摆脱了运行时错误,如下所示:

    headerView.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT, ListView.LayoutParams.WRAP_CONTENT));
    

    在添加视图之前。原因可在 Android 文档中找到:

    http://developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams)

    其中指出:

    这些提供参数给 此视图的 父级 指定应如何排列。有许多 ViewGroup.LayoutParams 的子类, 这些对应于不同的 ViewGroup 的子类是 负责安排他们的 孩子们。

    因此,基本上,如果您要向另一个视图添加视图,则必须将视图的 LayoutParams 设置为父级使用的 LayoutParams 类型,否则会出现运行时错误。

    【讨论】:

    • 我花了两个小时试图弄清楚这一点。谢谢!
    • 感谢您在这里发布答案,非常有帮助!
    • +1 花时间解释这一点.. 无论如何,没有人有很好的答案
    • 感谢您的解释
    【解决方案2】:

    在我的情况下,这不起作用。在设置它的适配器之前,我必须为我的 listView 设置一个 footerView。首先,我在 OnCreate 方法中从布局文件初始化了 loadingView:

    LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    loadingView = layoutInflater.inflate(R.layout.loading_view, null);
    

    然后我以相同的方法使用了这个解决方法:

    this.setIsLoading(true);
    listView.setAdapter(adapter);
    this.setIsLoading(false);
    

    在哪里

    private void setIsLoading(boolean isLoading)
    {
        this.isLoading = isLoading;
    
        if (isLoading) {
            listView.addFooterView(loadingView);
        }
        else {
            listView.removeFooterView(loadingView);
        }
    }
    

    【讨论】:

      【解决方案3】:

      尽管上述答案可能适用于许多人,但仍有更好的解释。 ClassCastException 是调用listview.addHeaderViewlistview.addFooterView 后的正常行为。

      原因是初始适配器被包裹在HeaderViewListAdapter 中。 要让您的适配器使用此代码。

      YourAdapter ya = (YourAdapter) ((HeaderViewListAdapter)lv.getAdapter()).getWrappedAdapter();
      

      Code taken from here

      【讨论】:

      • 您的解决方案优于 Christopher 解决方案。所以如果克里斯托弗的解决方案不起作用,试试这个家伙!
      【解决方案4】:

      你应该把父视图放到你的膨胀视图中。

      ListView listview = (ListView) findViewById(R.id.listview);
      headerView = View.inflate(this, R.layout.header, listview, false); // set listview as parent view
      listview.addHeaderView(headerview);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-18
        • 1970-01-01
        • 2017-10-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多