我无法将其设置为动态的Spinner!?
我要做的是为每个TableRow添加1个Spinner,具体取决于数据库中的行数(我不是只想显示数据库)。需要根据数据库中的数字设置微调器的id。
final String mArray[] = { "1", "2", "3" };
do{
int db_number = c.getInt(getColumnIndex("number"));
int db_type = c.getInt(getColumnIndex("type"));
Spinner sp_type = new Spinner(this);
sp_type.setId(db_number + 10000);
ArrayAdapter<String> adapter = newArrayAdapter<String(this,android.R.layout.simple_spinner_item,
mArray);
sp_type.setAdapter(adapter);
if (db_type == 1) {
sp_type.setSelection(0);
} else if (db_type == 2) {
sp_type.setSelection(1);
} else {
sp_type.setSelection(2);
}
tr.addView(sp_type);
}while(c.moveToNext());
这会导致应用崩溃。但是,如果我删除行“sp_type.setId(db_number + 10000);”我没有收到错误。
这是logCat:
06-05 01:21:09.936: E/AndroidRuntime(815): FATAL EXCEPTION: main
06-05 01:21:09.936: E/AndroidRuntime(815): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.edjr/com.edjr.Settings_EditDb}: java.lang.ClassCastException: android.widget.AbsSpinner$SavedState
06-05 01:21:09.936: E/AndroidRuntime(815): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2832)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.app.ActivityThread.access$1600(ActivityThread.java:117)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.os.Looper.loop(Looper.java:123)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-05 01:21:09.936: E/AndroidRuntime(815): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 01:21:09.936: E/AndroidRuntime(815): at java.lang.reflect.Method.invoke(Method.java:507)
06-05 01:21:09.936: E/AndroidRuntime(815): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-05 01:21:09.936: E/AndroidRuntime(815): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-05 01:21:09.936: E/AndroidRuntime(815): at dalvik.system.NativeStart.main(Native Method)
06-05 01:21:09.936: E/AndroidRuntime(815): Caused by: java.lang.ClassCastException: android.widget.AbsSpinner$SavedState
06-05 01:21:09.936: E/AndroidRuntime(815): at android.widget.CompoundButton.onRestoreInstanceState(CompoundButton.java:339)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.view.View.dispatchRestoreInstanceState(View.java:6278)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1215)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1215)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1215)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1215)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1215)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1215)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1215)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.view.View.restoreHierarchyState(View.java:6257)
06-05 01:21:09.936: E/AndroidRuntime(815): at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1486)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.app.Activity.onRestoreInstanceState(Activity.java:844)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.app.TabActivity.onRestoreInstanceState(TabActivity.java:58)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.app.Activity.performRestoreInstanceState(Activity.java:816)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096)
06-05 01:21:09.936: E/AndroidRuntime(815): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1625)
06-05 01:21:09.936: E/AndroidRuntime(815): ... 12 more