ListView で機能するクラス Area 用のアダプターを持っていますが、このアダプターをスピナーに使用しようとするとエラーが発生します。スピナーを使用してアクティビティを実行すると、リストの最初のアイテムが表示されますが、矢印をクリックして他のアイテムも表示するとエラーが発生します。誰かが私が間違っていることを教えてもらえますか?
アダプター クラス:
public class AreaAdapter extends ArrayAdapter<Area> {
List<Area> areaList;
public AreaAdapter(Context context, int textViewResouceId,
List<Area> objects) {
super(context, textViewResouceId, objects);
areaList = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
Context c = getContext();
LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(
R.layout.activity_choose_area_adapter, null);
}
Area area = areaList.get(position);
if (area != null) {
TextView v = null;
v = (TextView) convertView.findViewById(R.id.tareaname);
v.setText(area.getAreaName());
}
return convertView;
}
}
アダプターの XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.72"
android:orientation="vertical" >
<TextView
android:id="@+id/tareaname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
スピナーを含むアクティビティ:
public class ExtrasAddItemActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_extras_add_item);
ActivitiesObjects ao = (ActivitiesObjects) this.getApplication();
List<Area> listOfAreas = ao.getListOfAreas();
Spinner spinnerArea = (Spinner)findViewById(R.id.spinnerExtraArea);
AreaAdapter areaAdapter = new AreaAdapter(this,R.layout.activity_choose_area_adapter, listOfAreas);
spinnerArea.setAdapter(areaAdapter);
}
}
スピナーを使用した XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
// other widgets ...
<Spinner
android:id="@+id/spinnerExtraArea"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
エラーログ:
09-05 13:36:05.347: D/dalvikvm(274): GC_EXTERNAL_ALLOC freed 813 objects / 59328 bytes in 54ms
09-05 13:36:10.487: W/KeyCharacterMap(274): No keyboard for id 0
09-05 13:36:10.487: W/KeyCharacterMap(274): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-05 13:36:13.058: E/ArrayAdapter(274): You must supply a resource ID for a TextView
09-05 13:36:13.058: D/AndroidRuntime(274): Shutting down VM
09-05 13:36:13.068: W/dalvikvm(274): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-05 13:36:13.101: E/AndroidRuntime(274): FATAL EXCEPTION: main
09-05 13:36:13.101: E/AndroidRuntime(274): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:329)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:325)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.ListView.onMeasure(ListView.java:1109)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.view.View.measure(View.java:8171)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.view.View.measure(View.java:8171)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
09-05 13:36:13.101: E/AndroidRuntime(274): at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.view.View.measure(View.java:8171)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.view.View.measure(View.java:8171)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.view.View.measure(View.java:8171)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.os.Looper.loop(Looper.java:123)
09-05 13:36:13.101: E/AndroidRuntime(274): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-05 13:36:13.101: E/AndroidRuntime(274): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 13:36:13.101: E/AndroidRuntime(274): at java.lang.reflect.Method.invoke(Method.java:521)
09-05 13:36:13.101: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-05 13:36:13.101: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-05 13:36:13.101: E/AndroidRuntime(274): at dalvik.system.NativeStart.main(Native Method)
09-05 13:36:13.101: E/AndroidRuntime(274): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
09-05 13:36:13.101: E/AndroidRuntime(274): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
09-05 13:36:13.101: E/AndroidRuntime(274): ... 34 more
前もって感謝します、アレックス