按钮因为一些奇怪的原因被投到一个复选框上?

问题描述 投票:2回答: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>

这个备用的设置会使应用程序崩溃,并出现下面所示的错误日志(似乎按钮被类型化为复选框,而我却没有尝试? 我发现这个错误真的很奇怪,想知道是否有一个我不理解的androidxmlUI的基本部分使得这种情况发生。

<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
android android-layout android-widget android-ui android-xml
2个回答
3
投票

这个问题可能发生在改变布局XML文件的方式,一些widget保持它的ID,但改变类型(类)。

我的猜测是 R 类没有被正确,由EclipseAndroid插件自动重建。

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


0
投票

使用 "import com.rey.material.widget.CheckBox; "而不是 "import android.widget.CheckBox; "对我来说是有效的。

© www.soinside.com 2019 - 2024. All rights reserved.