我不知道发生了什么,但是当我在手机上测试运行我的应用程序时,我仍然会收到此错误。
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:197)
at android.widget.Spinner.onMeasure(Spinner.java:507)
at android.view.View.measure(View.java:17448)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17448)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17448)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:17448)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17448)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17448)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17448)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2031)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1189)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1402)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5884)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:197)
at android.widget.Spinner.onMeasure(Spinner.java:507)
at android.view.View.measure(View.java:17448)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17448)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17448)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:17448)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17448)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17448)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5465)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17448)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2031)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1189)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1402)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5884)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
我尝试了很多选项,包括为我的微调器arraylist创建一个baseAdapter,但似乎没有任何效果。这是我的自定义arrayadapter,我认为是问题,但我不知道:
public class SpinnerArrayAdapter extends ArrayAdapter<ContactsData> {
...
public View getCustomView(int position, ViewGroup parent, View convertView){
View row = convertView;
ContactsHolder holder = null;
if(row == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(R.layout.row, parent, false);
holder = new ContactsHolder();
holder.name = (TextView)row.findViewById(R.id.contactName);
holder.number = (TextView)row.findViewById(R.id.contactPhone);
row.setTag(holder);
}else{
holder = (ContactsHolder)row.getTag();
}
ContactsData contactsData = data.get(position);
holder.name.setText(contactsData.name);
holder.number.setText(contactsData.number);
return row;
}
static class ContactsHolder{
TextView name;
TextView number;
}
这是我的布局row.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:id="@+id/contactName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/contactPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
提前致谢。
在mainactivity.java中初始化arrayadapter
ArrayList<ContactsData> contacts_data = new ArrayList<>();
contacts_data.add(new ContactsData("lol", "2"));
SpinnerArrayAdapter mAdapter = new SpinnerArrayAdapter(MainActivity.this, R.layout.row, contacts_data);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(mAdapter);
答案 0 :(得分:1)
您需要添加您想要使用的TextView
的ID
E.g。
SpinnerArrayAdapter mAdapter = new SpinnerArrayAdapter(MainActivity.this, R.layout.row, R.id. contactName, contacts_data);
当然
public class SpinnerArrayAdapter extends ArrayAdapter<ContactsData> {
public SpinnerArrayAdapter(Context c, int layoutId, int textViewId, ArrayList<ContactsData> data) {
super(c, layoutId, textViewId, data);
// other stuff
}
}
这只是超类的解决方法,因为您正在处理getView
答案 1 :(得分:0)
您的布局文件应该只包含一个textview,并且不能在任何布局下包装,如下面的
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
// other attributes of the TextView
/>