- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个列表适配器,我正在尝试为 (item.xml) 设置自定义 xml如果我不使用通货膨胀(通过应用程序上下文直接识别 TextView ),它可以完美地工作..但是我的 ListView 不能被主题化..
public View getView(int position, View convertView, ViewGroup parent) {
//System.gc();
TextView tv;
LayoutInflater inflater = getLayoutInflater();
View row = View.inflate(mContext,R.layout.item,null); //.inflate(R.layout.item, parent, false); doesnt work either..
String id = null;
if (convertView == null) {
tv = (TextView) row.findViewById(R.id.TextView01);;
} else
tv = (TextView) convertView;
[..]
在 logcat
我得到了这个..
07-15 19:45:51.710: ERROR/AndroidRuntime(20185): FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
at android.widget.ListView.setupChild(ListView.java:1827)
at android.widget.ListView.makeAndAddView(ListView.java:1794)
at android.widget.ListView.fillDown(ListView.java:688)
我的 item.xml 文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:gravity="left|center"
android:layout_width="fill_parent"
android:paddingBottom="15px"
android:background="#fff200"
android:paddingTop="15px"
android:paddingLeft="15px">
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40px"
android:textStyle="bold"
android:layout_marginLeft="20px"
android:textColor="#0099CC">
</TextView>
</LinearLayout>
最佳答案
问题是由于将您从适配器返回的控件包装在线性布局中引起的。从您的布局文件中丢失 LinearLayout(只需将 TextView 作为根),我认为它应该可以工作。
稍微扩展:您正在创建控件,然后将其添加到 LinearLayout。此时为其分配了一个 LinearLayout$LayoutParams 对象并将其设置为它的布局参数。然后你把它从线性布局中拉出来,放到你的 ListView 中,它不理解 LinearLayout$LayoutParams。
或者,从 getView 返回 row 而不是(正如我假设你现在正在做的那样,因为你已经从引用的代码中删除了返回行)返回 tv。
(另一个不相关的提示:您正在膨胀布局,然后在 convertView != null 时将其丢弃;这将导致慢速设备上的滚动性能不佳。仅在您确实需要时才执行膨胀。)
关于Android - ClassCastException - 在从 LinearLayout 到 AbsListView 的 LayoutParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11492678/
我试图在 2 个小部件/布局之间创建一个 float 操作按钮 我 99% 确定我已经使用了这个教程并且有效 How can I add the new "Floating Action Button
这是 XML 代码,
因为它可以与Relative 或Linear 布局一起使用。因此,如果我们稍后将 RelativeLayout 更改为 LinearLayout,它可能会很有用。 LinearLayout.Layou
我正在尝试开发 Android 3.1 平板电脑应用程序。 这个app会有很多图片,我已经关注了Processing Bitmaps Off the UI Thread教程,但我做错了,因为我得到:
我的初始代码行位于重写方法 onGlobalLayout() 中: img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 10
我正尝试按照发布在以下位置的示例编写周历: How can I create a weekly calendar view for an Android Honeycomb application?
这段代码似乎适用于 android api 16 - 22,但不适用于 api 23。我只是想显示一个带有选项的弹出窗口,并使弹出窗口下方的背景变暗: WindowPo
带有 Android Studio 2.0 的 OS X Yosemite 10.10.5 出于教学目的,我想将尽可能多的 View 属性从 XML 移动到 Java。 我在 GridLayout 中
说明及解决方法在底部。 我正在开发一个 slider 布局动画,动画效果很好,但是当所有进程结束时,它们会得到下一个异常。 我猜 RelativeLayout 父级在异常中有一些事情要做,但我不知道如
我正在尝试创建一个 FloatingActionButton,如 this , 但是当我运行应用程序时出现以下错误 java.lang.ClassCastException: android.wid
根据大量教程和示例项目,我得到了以下代码,它应该用包含来自 Line 对象的多个值的行填充 ListView。 作为一名新手程序员,我一辈子都弄不明白我的代码到底哪里出错了。任何帮助将不胜感激! 如果
我得到了这个异常(exception): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cas
Android Studio 3.1、Java 1.8、Gradle 4.1。我使用 minVersion = 15 我想在文件 profile_category_active.xml 中以编程方式设
我正在尝试使我的整个布局(其中包含 ListView)可滚动。也就是说,整个布局应该拥有滚动条,而不仅仅是 ListView。我正在尝试使用 commonsguy 的 MergeAdapter ( h
我遇到了以下错误 java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to an
还有其他相关问题,但它们没有解决我的情况(它们的错误代码与客户端代码的某种回收和/或错误的转换调用有关)。 我的情况比较复杂。 我有一些代码,用户可以在其中调出照片库。 问题是,它在我拥有的 7 部手
我正在尝试做抽屉导航选项。 这是我的 xml: 如果我删除 Linearlayout
我正在尝试获取 AppBarLayout 的布局参数,但出现此错误。 java.lang.ClassCastException: android.support.design.widget.Coord
我使用一个具有RelativeLayout -> ScrollView -> LinearLayout 的 View 。我得到了异常(exception): 11-22 11:17:24.605: E
我在调试我正在处理的 Android 应用程序时遇到了问题。目前,它应该能够从文本文件中获取输入(它可以),将其转换为包含表示网格部分的 Cell 对象的 ArrayList(它确实如此),然后使用
我是一名优秀的程序员,十分优秀!