You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中ListView运行崩溃(Unfortunately Stopped Working)求助

Android ListView 应用崩溃排查

我是Android开发新手,跟着WSCube的视频学习ListView组件,也看了其他YouTube视频,但始终解决不了应用运行崩溃(提示“Unfortunately Stopped Working”)的问题。以下是我编写的全部代码,麻烦帮忙排查并给出建议。

Android Manifest

<?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>

activity_main.xml

<?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>

MainActivity.java

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);
    }
}

崩溃原因

代码里的致命错误出在ListView的实例化步骤:

listView.findViewById(R.id.listView);

这里你调用了findViewById但没有把返回的ListView对象赋值给listView变量,导致listView一直为null。后续调用listView.setAdapter(adapter)时,就会触发NullPointerException,直接导致应用崩溃。

修复方案

把ListView实例化的代码改为:

listView = findViewById(R.id.listView);

额外优化建议

  1. 调试时优先查看Logcat中的错误日志,里面会明确标注崩溃类型(比如NullPointerException)和具体行号,这是排查问题最直接的方式。
  2. 用当前Activity的上下文this代替getApplicationContext()创建ArrayAdapter,更贴合页面生命周期,减少潜在内存泄漏风险:
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrNames);
    
  3. 将ListView的高度改为match_parent,wrap_content在数据量较大时可能会出现显示异常:
    android:layout_height="match_parent"
    

内容的提问来源于stack exchange,提问作者Zahir Anwar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 19:10:20