gpt4 book ai didi

Android ListView 的麻烦

转载 作者:行者123 更新时间:2023-11-30 03:13:26 25 4
gpt4 key购买 nike

我在尝试运行实现 ListView、用 20 个元素填充并显示它的 Android Activity 时遇到问题: Activity 在启动时崩溃。

以下是相关的 LogCat 错误:

You must supply a resource ID for a TextView
FATAL EXCEPTION: main
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.AbsListView.obtainView(AbsListView.java:2177)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
at android.widget.ListView.onMeasure(ListView.java:15848)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
at android.widget.RelativeLayout.onMeasure(ListView.java:1159)
at android.view.View.measure(View.java:15848)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.app.ActivityThread.main(ActivityThread.java:5013)
Caused by: java.lang.ClassCastException: android.widget.ListView cannot be cast to android.widget.TextView.
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:382)

ThirdActivity.java

package com.example.logger;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class ThirdActivity extends ListActivity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
final ListView listview = (ListView) findViewById(android.R.id.list);
String[] values = new String[] {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen",
"Twenty"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_third, android.R.id.list, values);
setListAdapter(adapter);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.third, menu);
return true;

}

public void attachButton()
{
findViewById(R.id.add).setOnClickListener(this);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}
}

结束.java

布局:

Activity_third.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ThirdActivity" >

<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/add"
/>

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_below="@+id/add"
android:layout_height="wrap_content"

></ListView>

</RelativeLayout>

结束.xml

能否根据 LogCat 错误日志告诉我需要更改哪些内容才能使其正常工作?

非常感谢您的帮助。

您真诚的,

毛罗。

最佳答案

ArrayAdapter requires the resource ID to be a TextView

这只是意味着,您必须提供一个带有 TextView 作为参数的布局。适配器会将此 TextView 绑定(bind)到您的数据。但是您当前传递的是包含 ListView 的布局。

所以改变这个:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_third, android.R.id.list, values);

为此:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);

关于Android ListView 的麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20547333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com