java.lang.ClassCastException:android.widget.LinearLayout无法使用ArrayAdapter强制转换为android.widget.TextView
我正在做在安卓设备上开发的学期项目。 问题是我试图将一个对象添加到ArrayAdapter中。 但它得到了一个错误java.lang.ClassCastException:android.widget.LinearLayout无法使用ArrayAdapter强制转换为android.widget.TextView,java,android,xml,android-arrayadapter,Java,Android,Xml,Android Arrayadapter,我正在做在安卓设备上开发的学期项目。 问题是我试图将一个对象添加到ArrayAdapter中。 但它得到了一个错误 11-29 11:58:07.136: D/dalvikvm(1262): GC_FOR_ALLOC freed 136K, 8% free 2677K/2880K, paused 30ms, total 32ms 11-29 11:58:07.415: E/ArrayAdapter(1262): You must supply a resource ID for a TextV
11-29 11:58:07.136: D/dalvikvm(1262): GC_FOR_ALLOC freed 136K, 8% free 2677K/2880K, paused 30ms, total 32ms
11-29 11:58:07.415: E/ArrayAdapter(1262): You must supply a resource ID for a TextView
11-29 11:58:07.415: D/AndroidRuntime(1262): Shutting down VM
11-29 11:58:07.415: W/dalvikvm(1262): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-29 11:58:07.465: E/AndroidRuntime(1262): FATAL EXCEPTION: main
11-29 11:58:07.465: E/AndroidRuntime(1262): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.widget.AbsListView.obtainView(AbsListView.java:2177)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.widget.ListView.makeAndAddView(ListView.java:1840)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.widget.ListView.fillDown(ListView.java:675)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.widget.ListView.fillFromTop(ListView.java:736)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.widget.ListView.layoutChildren(ListView.java:1655)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.widget.AbsListView.onLayout(AbsListView.java:2012)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.View.layout(View.java:14289)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.View.layout(View.java:14289)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.View.layout(View.java:14289)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-29 11:58:07.465: E/AndroidRuntime(1262): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.View.layout(View.java:14289)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.View.layout(View.java:14289)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.Choreographer.doFrame(Choreographer.java:532)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.os.Handler.handleCallback(Handler.java:730)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.os.Handler.dispatchMessage(Handler.java:92)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.os.Looper.loop(Looper.java:137)
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-29 11:58:07.465: E/AndroidRuntime(1262): at java.lang.reflect.Method.invokeNative(Native Method)
11-29 11:58:07.465: E/AndroidRuntime(1262): at java.lang.reflect.Method.invoke(Method.java:525)
11-29 11:58:07.465: E/AndroidRuntime(1262): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-29 11:58:07.465: E/AndroidRuntime(1262): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-29 11:58:07.465: E/AndroidRuntime(1262): at dalvik.system.NativeStart.main(Native Method)
11-29 11:58:07.465: E/AndroidRuntime(1262): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
11-29 11:58:07.465: E/AndroidRuntime(1262): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
11-29 11:58:07.465: E/AndroidRuntime(1262): ... 40 more
这是我的主要活动-onCreate方法。
private EditText createTaskText;
ArrayAdapter<Task> listAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // To change body of overridden
// methods use File | Settings |
// File Templates.
setContentView(R.layout.main);
// !!! Add quick create task !!!
createTaskText = (EditText) findViewById(R.id.quickTopic);
TextView.OnEditorActionListener exListener = new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_NULL
&& event.getAction() == KeyEvent.ACTION_DOWN
&& !createTaskText.getText().toString().equals("")) {
// setContentView(R.layout.edit_task_page);
Intent goTaskEdit = new Intent(ExpandAnimationDemo.this,
TaskEdit.class);
goTaskEdit.putExtra("Topic", createTaskText.getText()
.toString());
startActivity(goTaskEdit);
createTaskText.setText("");
}
return false;
}
};
createTaskText.setOnEditorActionListener(exListener);
// Creating list, initially = 0 list
ListView list = (ListView) findViewById(R.id.udiniList);
Task[] items = {
new Task("Task 1"),
new Task("Task 2"),
new Task("Task 3"),
new Task("Task 4"),
new Task("Task 5"),
};
// Creating the list adapter and populating the list
listAdapter = new ArrayAdapter<Task>(this,
R.layout.list_item, items);
list.setAdapter(listAdapter);
// Creating an item click listener, to open/close our toolbar for each
// item
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
View toolbar = view.findViewById(R.id.toolbar);
// Creating the expand animation for the item
ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500);
// Start the animation on the toolbar
toolbar.startAnimation(expandAni);
}
});
// !!! Set Swipe to Dismiss !!!
// Create a ListView-specific touch listener. ListViews are given special treatment because
// by default they handle touches for their list items... i.e. they're in charge of drawing
// the pressed state (the list selector), handling list item clicks, etc.
SwipeDismissListViewTouchListener touchListener =
new SwipeDismissListViewTouchListener(
list,
new SwipeDismissListViewTouchListener.DismissCallbacks() {
@Override
public boolean canDismiss(int position) {
return true;
}
@Override
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
listAdapter.remove(listAdapter.getItem(position));
}
listAdapter.notifyDataSetChanged();
}
});
list.setOnTouchListener(touchListener);
// Setting this scroll listener is required to ensure that during ListView scrolling,
// we don't look for swipes.
list.setOnScrollListener(touchListener.makeScrollListener());
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/quickTopic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:hint="@string/quickTaskTopic"
android:inputType="text" />
<ListView
android:id="@+id/udiniList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="20dip" />
<ImageView
android:id="@+id/editPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_toLeftOf="@+id/deletePic"
android:contentDescription="@string/EditDesc"
android:src="@drawable/pencil" />
<ImageView
android:id="@+id/deletePic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:contentDescription="@string/DeleteDesc"
android:src="@drawable/delete" />
</RelativeLayout>
<!-- *********************** -->
<!-- *** TOOLBAR LAYOUT **** -->
<!-- *********************** -->
<LinearLayout
android:id="@+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-20dip"
android:orientation="vertical"
android:visibility="gone" >
<!-- TextView will showing task details.The details must get from user database -->
<TextView
android:id="@+id/taskDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="20dp"
android:paddingLeft="50dp"
android:singleLine="false"
android:text="@string/Details" />
<!-- This RelativeLayout manage the TextView which represent the information of Priority, DueDate, and Tag s-->
<RelativeLayout
android:id="@+id/taskSpec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false" >
<!-- TextView of "Priority" and priority value -->
<TextView
android:id="@+id/PriorityStr"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="20dp"
android:paddingLeft="50dp"
android:text="@string/Priority" />
<TextView
android:id="@+id/PriorityGet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/PriorityStr"
android:padding="20dp"
android:paddingLeft="55dp"
android:text="@string/SamplePriority" />
<!-- TextView of "DueDate" and duedate value -->
<TextView
android:id="@+id/DueDateStr"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@+id/PriorityStr"
android:padding="20dp"
android:paddingLeft="50dp"
android:text="@string/DueDate" />
<TextView
android:id="@+id/DueDateGet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/PriorityGet"
android:layout_toRightOf="@+id/DueDateStr"
android:padding="20dp"
android:paddingLeft="50dp"
android:text="@string/SampleDueDate" />
<!-- TextView of "Tags" and tags value -->
<TextView
android:id="@+id/TagsStr"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@+id/DueDateStr"
android:padding="20dp"
android:paddingLeft="50dp"
android:text="@string/Tags" />
<TextView
android:id="@+id/TagGet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/DueDateGet"
android:layout_toRightOf="@+id/TagsStr"
android:padding="20dp"
android:paddingLeft="60dp"
android:text="@string/SampleTags" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/quickTopic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:hint="@string/quickTaskTopic"
android:inputType="text" />
<ListView
android:id="@+id/udiniList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="20dip" />
<ImageView
android:id="@+id/editPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_toLeftOf="@+id/deletePic"
android:contentDescription="@string/EditDesc"
android:src="@drawable/pencil" />
<ImageView
android:id="@+id/deletePic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:contentDescription="@string/DeleteDesc"
android:src="@drawable/delete" />
</RelativeLayout>
<!-- *********************** -->
<!-- *** TOOLBAR LAYOUT **** -->
<!-- *********************** -->
<LinearLayout
android:id="@+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-20dip"
android:orientation="vertical"
android:visibility="gone" >
<!-- TextView will showing task details.The details must get from user database -->
<TextView
android:id="@+id/taskDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="20dp"
android:paddingLeft="50dp"
android:singleLine="false"
android:text="@string/Details" />
<!-- This RelativeLayout manage the TextView which represent the information of Priority, DueDate, and Tag s-->
<RelativeLayout
android:id="@+id/taskSpec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false" >
<!-- TextView of "Priority" and priority value -->
<TextView
android:id="@+id/PriorityStr"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:padding="20dp"
android:paddingLeft="50dp"
android:text="@string/Priority" />
<TextView
android:id="@+id/PriorityGet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/PriorityStr"
android:padding="20dp"
android:paddingLeft="55dp"
android:text="@string/SamplePriority" />
<!-- TextView of "DueDate" and duedate value -->
<TextView
android:id="@+id/DueDateStr"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@+id/PriorityStr"
android:padding="20dp"
android:paddingLeft="50dp"
android:text="@string/DueDate" />
<TextView
android:id="@+id/DueDateGet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/PriorityGet"
android:layout_toRightOf="@+id/DueDateStr"
android:padding="20dp"
android:paddingLeft="50dp"
android:text="@string/SampleDueDate" />
<!-- TextView of "Tags" and tags value -->
<TextView
android:id="@+id/TagsStr"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@+id/DueDateStr"
android:padding="20dp"
android:paddingLeft="50dp"
android:text="@string/Tags" />
<TextView
android:id="@+id/TagGet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/DueDateGet"
android:layout_toRightOf="@+id/TagsStr"
android:padding="20dp"
android:paddingLeft="60dp"
android:text="@string/SampleTags" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
如何解决此问题?从错误中可以看出,
ArrayAdapter要求资源ID为TextView
试着改变
listAdapter = new ArrayAdapter<Task>(this, R.layout.list_item, items);
listAdapter=newarrayadapter(此,R.layout.list_项,项);
为此:
listAdapter = new ArrayAdapter<Task>(this, R.layout.list_item, R.id.TEXT_VIEW_ID, items);
listAdapter=newarrayadapter(这个,R.layout.list\u项,R.id.TEXT\u视图\u id,项);
将布局中的
R.id.TEXT\u VIEW\u id
替换为TextView id数组适配器在第二个参数中采用xml,该参数应仅包含TextView。您需要实现自己的自定义适配器,因为您的list\u项
包含大量要在ListView中膨胀的小部件
Android的ArrayAdapter无法做到这一点。您需要通过扩展ArrayAdapter或BaseAdapter来实现自己的功能
11-29 11:58:07.465: E/AndroidRuntime(1262): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
出现此错误是因为列表项父窗口小部件是一个线性布局
,而ArrayAdapter的第二个参数采用文本视图
xml。例如:
ArrayAdapter listAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1 items);
如果在简单列表项目1
上按住CTRL+键并单击
,将打开一个xml,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>
如您所见,它是一个单一的文本视图
因此,您需要实现自己的自定义适配器。能否请您共享ExpandAnimation类。正如logcat所说的
ArrayAdapter要求资源ID为TextView
。因此,问题在于ArrayAdapter
的实例化,而不是您在此处发布的代码。您的解决方案是工作!非常感谢@用户3050141我很高兴这有帮助:)谢谢你的解释和建议。我希望你的评论对其他人也有好处