Android Studio -列表视图错误(不幸停止工作)

jjhzyzn0  于 2023-01-31  发布在  Android
关注(0)|答案(1)|浏览(142)

我是新的android开发和学习列表视图从WSCube。我已经看到了其他几个youtube视频也没有找到解决方案。请检查和建议。
我已经附上了所有的代码比我通过观看WsCube YouTube频道的视频在我的系统上工作.
enter image description here

Android清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ListView"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"></action>
                <category android:name="android.intent.category.LAUNCHER"></category>
            </intent-filter>
        </activity>
    </application>

</manifest>

活动_主要

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

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

</LinearLayout>

主要活动

package com.example.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ListView listView;
    ArrayList<String> arrNames = new ArrayList<>();

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

        listView.findViewById(R.id.listView);

        arrNames.add("Daniel01");
        arrNames.add("Daniel02");
        arrNames.add("Daniel03");
        arrNames.add("Daniel04");
        arrNames.add("Daniel05");
        arrNames.add("Daniel06");
        arrNames.add("Daniel07");
        arrNames.add("Daniel08");
        arrNames.add("Daniel09");
        arrNames.add("Daniel10");
        arrNames.add("Daniel11");
        arrNames.add("Daniel12");
        arrNames.add("Daniel13");
        arrNames.add("Daniel14");
        arrNames.add("Daniel15");
        arrNames.add("Daniel16");
        arrNames.add("Daniel17");
        arrNames.add("Daniel18");
        arrNames.add("Daniel19");
        arrNames.add("Daniel20");

        ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, arrNames);
        listView.setAdapter(adapter);
    }
}
elcex8rz

elcex8rz1#

你犯了个错误。

listView.findViewById(R.id.listView);

这是错误的,因为你在listViewfindViewById(R.id.listView);之间使用了一个点.
你需要初始化变量listView,你可以用等于=来初始化它,看我给的例子。

溶液

listView = findViewById(R.id.listView);

相关问题