1

ダイナミックスピナーにsetIdを設定できません!?

私がやろうとしているのは、データベースの行数に応じて、TableRowごとに1つのスピナーを追加することです(データベースを表示するだけではありません)。スピナーの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
4

0 に答える 0