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);
额外优化建议
- 调试时优先查看Logcat中的错误日志,里面会明确标注崩溃类型(比如NullPointerException)和具体行号,这是排查问题最直接的方式。
- 用当前Activity的上下文
this代替getApplicationContext()创建ArrayAdapter,更贴合页面生命周期,减少潜在内存泄漏风险:ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrNames); - 将ListView的高度改为
match_parent,wrap_content在数据量较大时可能会出现显示异常:android:layout_height="match_parent"
内容的提问来源于stack exchange,提问作者Zahir Anwar
相关产品推荐
相关产品推荐

