我正在编写一个程序,我正在使用Spinner,之前我在我的项目中使用了相同类型的微调器,但没有遇到任何问题,不知道为什么它会发生在我身上!
我得到了:
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
日志:
02-24 01:01:13.793: E/AndroidRuntime(1561): FATAL EXCEPTION: main
02-24 01:01:13.793: E/AndroidRuntime(1561): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.widget.AbsListView.obtainView(AbsListView.java:2177)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.widget.ListView.onMeasure(ListView.java:1159)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.View.measure(View.java:15848)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.View.measure(View.java:15848)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.View.measure(View.java:15848)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1451)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.View.measure(View.java:15848)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.View.measure(View.java:15848)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
02-24 01:01:13.793: E/AndroidRuntime(1561): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.View.measure(View.java:15848)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-24 01:01:13.793: E/AndroidRuntime(1561): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.View.measure(View.java:15848)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.Choreographer.doFrame(Choreographer.java:532)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.os.Handler.handleCallback(Handler.java:730)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.os.Looper.loop(Looper.java:137)
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-24 01:01:13.793: E/AndroidRuntime(1561): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 01:01:13.793: E/AndroidRuntime(1561): at java.lang.reflect.Method.invoke(Method.java:525)
02-24 01:01:13.793: E/AndroidRuntime(1561): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-24 01:01:13.793: E/AndroidRuntime(1561): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-24 01:01:13.793: E/AndroidRuntime(1561): at dalvik.system.NativeStart.main(Native Method)
02-24 01:01:13.793: E/AndroidRuntime(1561): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
02-24 01:01:13.793: E/AndroidRuntime(1561): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
02-24 01:01:13.793: E/AndroidRuntime(1561): ... 41 more
02-24 01:01:17.584: I/Process(1561): Sending signal. PID: 1561 SIG: 9
XML:
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/header"
android:layout_marginTop="21dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:paddingTop="2dp"
android:textStyle="bold"
android:textSize="20sp"
android:text="@string/food_pet_name" />
<EditText
android:id="@+id/editPetName"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="Pet Name"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/linearLayout1"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:paddingTop="2dp"
android:textStyle="bold"
android:textSize="20sp"
android:text="@string/food_desired_product" />
<Spinner
android:id="@+id/spinnerProduct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</LinearLayout>
几行:
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String> (getActivity(), android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
使用图形布局获取:
缺少样式。是否为此布局选择了正确的主题?
无法在当前主题中找到样式'textViewStyle'
无法在当前主题中找到样式'editTextStyle'
无法在当前主题中找到样式'spinnerStyle'
无法在当前主题中找到样式'buttonStyle'
android.content.res.Resources $ NotFoundException 属性“minHeight”中的“?adnroid:attr / listPrefferedItemHeight”不是有效格式。
注意: - 在这个项目中,我使用两个xmls的微调器,并且两个都有相同的问题。
答案 0 :(得分:0)
使用
public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)
指定TextView id。现在适配器认为simple_spinner_item布局的roow视图是TextView。但事实并非如此。
答案 1 :(得分:0)
您用于ArrayAdapter的构造函数是
public ArrayAdapter(Context context,int resource,T [] objects)在API级别1中添加
构造强>
context - 当前上下文。
资源 - 包含在实例化视图时使用的TextView的布局文件的资源ID。
对象 - 要在ListView中表示的对象。
其中int资源是包含TextView的布局的ID,它是您的RelativeLayout。但是,您传递的是android.R.layout.simple_spinner_item
。
所以尝试将其更改为,
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String> (getActivity(), your raletive layout ID, R.id.textView1 ,categories);
而且,如果您将自己的实现提供给ArrayAdapter,您也可以使用@ Shayan的答案。