Search code examples
androiduser-interfacecheckboxtablerow

Two Checkboxes in a single TableRow?


I am trying to fit two CheckBoxes into a single TableRow, so that they sit side by side. My program crashes at run time as soon as I try to load the view.

I previously had them each in their own row, but want to change them to fitting in the same row. I have messed with the android:layout_width feature but to no avail. This is the code I have for the TableRow.

         <TableRow>
            <CheckBox 
                android:id="@+id/circleCloseCheck"
                android:text="@string/parameters_circle_close"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <CheckBox
                android:id="@+id/nwpCheck"
                android:text="@string/parameters_nwp_check"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </TableRow>

This is my LogCat...

08-23 08:59:20.760: W/dalvikvm(27494): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-23 08:59:20.768: E/AndroidRuntime(27494): FATAL EXCEPTION: main
08-23 08:59:20.768: E/AndroidRuntime(27494): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.eti.commander/my.eti.commander.Parameters}: java.lang.ClassCastException: android.widget.CheckBox
08-23 08:59:20.768: E/AndroidRuntime(27494):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-23 08:59:20.768: E/AndroidRuntime(27494):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-23 08:59:20.768: E/AndroidRuntime(27494):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-23 08:59:20.768: E/AndroidRuntime(27494):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-23 08:59:20.768: E/AndroidRuntime(27494):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 08:59:20.768: E/AndroidRuntime(27494):    at android.os.Looper.loop(Looper.java:130)
08-23 08:59:20.768: E/AndroidRuntime(27494):    at android.app.ActivityThread.main(ActivityThread.java:3683)
08-23 08:59:20.768: E/AndroidRuntime(27494):    at java.lang.reflect.Method.invokeNative(Native Method)
08-23 08:59:20.768: E/AndroidRuntime(27494):    at java.lang.reflect.Method.invoke(Method.java:507)
08-23 08:59:20.768: E/AndroidRuntime(27494):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-23 08:59:20.768: E/AndroidRuntime(27494):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-23 08:59:20.768: E/AndroidRuntime(27494):    at dalvik.system.NativeStart.main(Native Method)
08-23 08:59:20.768: E/AndroidRuntime(27494): Caused by: java.lang.ClassCastException: android.widget.CheckBox
08-23 08:59:20.768: E/AndroidRuntime(27494):    at my.eti.commander.Parameters.populateArrayLists(Parameters.java:534)
08-23 08:59:20.768: E/AndroidRuntime(27494):    at my.eti.commander.Parameters.onCreate(Parameters.java:54)
08-23 08:59:20.768: E/AndroidRuntime(27494):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-23 08:59:20.768: E/AndroidRuntime(27494):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-23 08:59:20.768: E/AndroidRuntime(27494):    ... 11 more

EDIT :

Here is the information for line 534 in my Parameters.java file. Line 534 is wattVarCurrentSpinner = (Spinner) findViewById( R.id.wattVarCurrentSpinner.

    wattVarCurrentRelayList = new ArrayList<String>();
    wattVarCurrentRelayList.add( "N/A" );
    for( int i = 1; i <= 100; i++ ) {
        double temp = (i/10.0);
        wattVarCurrentRelayList.add( String.valueOf( temp ) );
    }
    wattVarCurrentSpinner = (Spinner) findViewById( R.id.wattVarCurrentSpinner );
    if( RelayAPIModel.AppPrefsType.displayUnits.equals( "Relay" ) ) {
        wattVarCurrentAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_spinner_item, wattVarCurrentRelayList );
    } else if( RelayAPIModel.AppPrefsType.displayUnits.equals( "Protector" ) ) {
        wattVarCurrentAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_spinner_item, wattVarCurrentProtectorList );
    } else if( RelayAPIModel.AppPrefsType.displayUnits.equals( "Percent" ) ) {
        wattVarCurrentAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_spinner_item, wattVarCurrentPercentList );      
    } else {
        RelayAPIModel.AppPrefsType.displayUnits = "Relay";
        wattVarCurrentAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_spinner_item, wattVarCurrentRelayList );
    }
    wattVarCurrentAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
    wattVarCurrentSpinner.setAdapter( wattVarCurrentAdapter );

Solution

  • I think you forgot to add layout_width and layout_height for TableRow Also need to change the check box layout_width to "wrap_content"

        <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    
        <CheckBox
            android:id="@+id/circleCloseCheck"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/parameters_circle_close" />
    
        <CheckBox
            android:id="@+id/nwpCheck"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/parameters_nwp_check" />
    </TableRow>