我正在研究在Android设备上开发的术语项目。 问题是我尝试将一个对象添加到我的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 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
这是我的MainActivity - 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());
}
我的任务类
public class Task extends HistoryTask {
int priority;
Calendar duedate;
//------------------------------
// constructors
public Task() {
duedate = Calendar.getInstance();
}
public Task( String t ) {
super( t );
duedate = Calendar.getInstance();
}
public Task( String t, String d ) {
super( t, d );
duedate = Calendar.getInstance();
}
public Task( String t, String d, int tg ) {
super( t, d, tg );
duedate = Calendar.getInstance();
}
public Task( String t, String d, int tg, int p ) {
super( t, d, tg );
priority = p;
duedate = Calendar.getInstance();
}
public Task( String t, String d, int tg, int p, Calendar dd ) {
super( t, d, tg );
priority = p;
duedate = dd;
}
@Override
public String toString() {
return super.toString();
}
}
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>
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" >
<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>
我该如何解决这个问题?
答案 0 :(得分:3)
从错误中说出ArrayAdapter requires the resource ID to be a TextView
尝试更改
listAdapter = new ArrayAdapter<Task>(this, R.layout.list_item, items);
到此:
listAdapter = new ArrayAdapter<Task>(this, R.layout.list_item, R.id.TEXT_VIEW_ID, items);
将R.id.TEXT_VIEW_ID
替换为布局中的TextView ID
答案 1 :(得分:1)
Array Adapter在第二个参数中使用xml,该参数应仅包含textView。您需要实现自己的自定义适配器,因为list_item
包含大量要在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
您收到此错误是因为您的list_item父Widget为 LinearLayout
,而ArrayAdapter的第二个参数为 textview
xml 。例如:
ArrayAdapter listAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1 items);
如果您在Click
上 CTRL + simple_list_item_1
,则会打开一个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"
/>
如您所见,它是一个TextView,
因此,您需要实现自己的自定义适配器。