android-layout - 由于某种奇怪的原因,按钮被转换到复选框?

标签 android-layout android-widget android android-xml

我正在尝试获取 2 列复选框(不是表格布局),每行底部都有一个按钮。问题是,当我这样做时,按钮(在第一列中)看起来像是自动转换为复选框。然而,当我将按钮放在第二列时,似乎没有发生什么不好的事情。这是给我带来问题的第一列

下面的设置不会崩溃(尽管两个按钮都位于第二列而不是每列一个,这当然不是我想要的)。

<LinearLayout
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:orientation="horizontal"
       android:layout_weight="1.7"
       android:layout_marginTop="5dp"
       android:background="@drawable/table_layout_bg" 
       >


         <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:paddingTop="15dp"
            android:paddingBottom="20dp"
            android:paddingLeft="20dp">

            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/American_chkBX"  android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/Asian_chkBX"     android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/BarPub_chkBX"    android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/Breakfast_chkBX" android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/Desert_chkBX"    android:layout_width="fill_parent"/>


            </LinearLayout>

            <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:paddingTop="15dp"
            android:paddingBottom="20dp">

            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/FastFood_chkBX" android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/Healthy_chkBX" android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/Italian_chkBX" android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/Mexican_chkBX" android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/SeaFood_chkBX" android:layout_width="fill_parent"/>   

            <Button
                android:id="@+id/select_none_btn"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:background="@drawable/select_none"
                />  

            <Button
                android:id="@+id/select_all_btn"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:background="@drawable/select_all"
                />
            </LinearLayout>



     </LinearLayout>

这种替代设置会导致应用程序崩溃,并显示如下错误日志(似乎该按钮在我没有尝试的情况下就被类型转换为复选框??)。我发现这个错误真的很奇怪,想知道是否有一个我不明白的 android/xml/UI 的基本部分导致了这种情况的发生。

<LinearLayout
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:orientation="horizontal"
       android:layout_weight="1.7"
       android:layout_marginTop="5dp"
       android:background="@drawable/table_layout_bg" 
       >

   <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:paddingTop="15dp"
            android:paddingBottom="20dp"
            android:paddingLeft="20dp">

            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/A_chkBX"  android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/B_chkBX"     android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/C_chkBX"    android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/D_chkBX" android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/E_chkBX"    android:layout_width="fill_parent"/>

            <Button
                android:id="@+id/select_all_btn"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:background="@drawable/select_all"
                />
            </LinearLayout>

            <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:paddingTop="15dp"
            android:paddingBottom="20dp">

            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/A1_chkBX" android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/B1_chkBX" android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/C1_chkBX" android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/D1_chkBX" android:layout_width="fill_parent"/>
            <CheckBox android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/E1_chkBX" android:layout_width="fill_parent"/>    

            <Button
                android:id="@+id/select_none_btn"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:background="@drawable/select_none"
                />  
            </LinearLayout>
 </LinearLayout>

以下是生成的相关日志:

03-26 22:21:30.279: E/AndroidRuntime(1473): FATAL EXCEPTION: main
03-26 22:21:30.279: E/AndroidRuntime(1473): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dumbasssoftware/com.dumbasssoftware.mainstuff.PickerViewActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.CheckBox
03-26 22:21:30.279: E/AndroidRuntime(1473):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-26 22:21:30.279: E/AndroidRuntime(1473):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-26 22:21:30.279: E/AndroidRuntime(1473):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-26 22:21:30.279: E/AndroidRuntime(1473):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-26 22:21:30.279: E/AndroidRuntime(1473):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 22:21:30.279: E/AndroidRuntime(1473):     at android.os.Looper.loop(Looper.java:137)
03-26 22:21:30.279: E/AndroidRuntime(1473):     at android.app.ActivityThread.main(ActivityThread.java:4424)
03-26 22:21:30.279: E/AndroidRuntime(1473):     at java.lang.reflect.Method.invokeNative(Native Method)
03-26 22:21:30.279: E/AndroidRuntime(1473):     at java.lang.reflect.Method.invoke(Method.java:511)
03-26 22:21:30.279: E/AndroidRuntime(1473):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-26 22:21:30.279: E/AndroidRuntime(1473):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-26 22:21:30.279: E/AndroidRuntime(1473):     at dalvik.system.NativeStart.main(Native Method)
03-26 22:21:30.279: E/AndroidRuntime(1473): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.CheckBox
03-26 22:21:30.279: E/AndroidRuntime(1473):     at com.dumbasssoftware.mainstuff.PickerViewActivity.onCreate(PickerViewActivity.java:708)
03-26 22:21:30.279: E/AndroidRuntime(1473):     at android.app.Activity.performCreate(Activity.java:4465)
03-26 22:21:30.279: E/AndroidRuntime(1473):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-26 22:21:30.279: E/AndroidRuntime(1473):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-26 22:21:30.279: E/AndroidRuntime(1473):     ... 11 more

最佳答案

当某些小部件保留其 ID 但更改类型(类)的方式更改布局 XML 文件时,可能会出现此问题。

我的猜测是 R 类没有正确地由 Eclipse/Android 插件自动重建。

修复方法是手动强制项目清理和重建。

关于android-layout - 由于某种奇怪的原因,按钮被转换到复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15648754/

相关文章:

Android ListView乱码 View

android - 如何以编程方式创建 android 形状背景?

android - 推特可以上传照片吗?如果是,那么如何从 Android 发布或上传照片?

Android - MaterialDatePicker 布局问题

android - 我不能使用 spinner.setAdapter(adapter);

Android 小部件禁用 listitem 嵌套按钮 onclick

android - 在图层列表中剪切平铺位图的角

android - TextView 与附近按钮的高度相同

android - 在 TextView 上调用 setText 会调用 requestLayout(),它会重置父 View 的 offsetTopAndBottom 位置

Android:从 View 传递触摸事件