【问题标题】:ViewBinder cast error with android 4.0android 4.0 的 ViewBinder 投射错误
【发布时间】:2012-04-10 08:41:44
【问题描述】:

我收到以下错误(代码中的第 19 行是 //XXX):

04-10 10:35:08.301: E/AndroidRuntime(12417): java.lang.ClassCastException: android.widget.TextView 无法转换 到 android.widget.CheckBox 04-10 10:35:08.301: E/AndroidRuntime(12417):在 ms.jung.andorid.caldavtodo.CalDavToDoViewBinder.setViewValue(CalDavToDoViewBinder.java:19)

我的代码:

class CalDavToDoViewBinder implements SimpleCursorAdapter.ViewBinder {

    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        int viewId = view.getId();

        if(viewId == R.id.checkBox) {
                CheckBox cb = (CheckBox) view; //XXX

                if(cursor.getInt(cursor.getColumnIndexOrThrow(CalDavToDoProvider.STATE)) == 1)
                {
                    cb.setChecked(true); 
                }
                else 
                {
                    cb.setChecked(false);
                }
                return true;

        }
        else if(viewId == R.id.colorBar)
        {

                int color = cursor.getInt(cursor.getColumnIndexOrThrow(CalDavToDoProvider.COLOR));

                TextView colorBar = (TextView)view;
                colorBar.setBackgroundColor(color);

                return true;    
        }

        return false;
    }
}

我很困惑,因为R.id.checkBox肯定是CheckBox

编辑:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="0dp" >

    <TextView
        android:id="@+id/colorBar"
        android:layout_width="10dp"
        android:layout_height="fill_parent"
        android:background="@color/pink"
        android:text="@string/colorBarDefault" >
    </TextView>

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:paddingLeft="45dp"
        android:text="@string/checkBoxDefault" >
    </CheckBox>

    <TextView
        android:id="@+id/sqlID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sqlIDDefault"
        android:visibility="gone" >
    </TextView>

</LinearLayout>

解决方案:

编辑:清理项目有帮助!

【问题讨论】:

    标签: android casting simplecursoradapter android-viewbinder


    【解决方案1】:

    使用view.setTag(objTextview);

    TextView colorBar = (TextView)view.getTag();
    

    【讨论】:

    • (?)种类的对象,即。 Textview、Button、任何控件或字符串、int、数组等...
    • 我应该把“use view.setTag(objTextview);”放在哪里完全在我的代码中?
    • R.id.colorBar 是一个TextView,但问题出在checkBox:R.id.checkBox
    • 你确定 R.id.checkBox 是 Checkbox 可能是你分配给 textview,只需清理你的项目运行它
    • 好的,清理项目完成了工作......感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多