我正在尝试将两个CheckBoxes
放入一个TableRow
中,以便它们并排放置。我尝试加载视图时,我的程序在运行时崩溃。
我以前将它们各自放在自己的行中,但是想要将它们更改为适合同一行。我搞砸了android:layout_width
功能,但无济于事。这是我对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>
这是我的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
编辑:
以下是我的Parameters.java文件中第534行的信息。第534行是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 );
答案 0 :(得分:1)
应该是每个复选框中的android:layout_width =“wrap_content”; 你还应该为每个复选框添加android:layout_weight =“1.0”
完美的例子在这里:SO answer
答案 1 :(得分:1)
我认为您忘记为 TableRow 添加layout_width和layout_height 还需要将复选框layout_width更改为“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>