我的自定义 ListView 行使应用程序崩溃?

My custom ListView row make app crash?

对 android 开发很陌生,我只是在使用标准布局 (android.R.layout.simple_list_item_1) 摆弄 ListViews,没问题。

但是当我尝试自定义布局时,我的应用程序在启动时崩溃。

首先是我的文件,所以你可以看到我在做什么(简单的东西只是为了习惯它)

Java 文件:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] testArray = {"test1", "test2", "test3", "test4", "test5", "test6", "test7"};

    ListAdapter theAdapter = new ArrayAdapter<String>(this, R.layout.row_layout_new, testArray);
    ListView theListView = (ListView) findViewById(R.id.theListView);
    theListView.setAdapter(theAdapter);

    theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String testArrayPicked = "You selected " +
                    String.valueOf(parent.getItemAtPosition(position));

            Toast.makeText(MainActivity.this, testArrayPicked, Toast.LENGTH_SHORT).show();
        }
    });
 }
}

查看(activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/theListView">
</ListView>

</LinearLayout>

我的布局文件(row_layout_new.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:id="@+id/TextViewNew"/>

</LinearLayout>

在我的 android 显示器上,我有以下内容:

E/ArrayAdapter: You must supply a resource ID for a TextView

但是我在我的行布局中给了 TextView 一个 id。

我看了一些教程(比如 Derek Banas youtube 频道,如果你们知道的话),他也是这样做的,但我这边崩溃了。

我是否遗漏了一些明显的东西,或者我必须制作自己的 ArrayAdapter 函数?

如错误所述,您需要将 TextView id 传递给 ArrayAdapter 才能使其正常运行。

改变

ListAdapter theAdapter = new ArrayAdapter<String>(this, R.layout.row_layout_new, testArray);

ListAdapter theAdapter = new ArrayAdapter<String>(this, R.layout.row_layout_new, R.id.TextViewNew, testArray);
ListAdapter theAdapter = new ArrayAdapter<String>(this, R.layout.row_layout_new, R.id.TextViewNew, testArray);

您还需要为您的构造函数提供正确的文本视图 ID。

在这一点上我可能大错特错,但我认为如果您想在传递给适配器的资源中使用 LinearLayout 作为根元素,则必须使用自定义适配器。如果你想坚持使用内置的 ArrayAdapter,你需要更改 row_layout_new.xml 以便它只包含一个 TextView。

编辑:或者,如本线程中的另一个答案所述,提供包含在 LinearLayout 中的 TextView 作为 ArrayAdapter 构造函数的另一个参数。

您应该为适配器提供文本视图的名称

ListAdapter theAdapter = new ArrayAdapter<String>(this, R.layout.row_layout_new, R.id.TextViewNew, testArray);

ListView(上下文上下文,AttributeSet 属性,int defStyleAttr,int defStyleRes)