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

Android音乐播放器内部存储音乐列表加载崩溃问题求助

排查与解决音乐列表Activity崩溃及ListView加载问题

Hey there, let's walk through the most likely issues causing your music list Activity to crash or fail to load content, along with actionable fixes:

1. First, Grab the Crash Logs (Critical!)

The first thing you need to do is check the Logcat in Android Studio. Filter by your app's package name and look for Error or Fatal entries—this will tell you exactly what's causing the crash (like a NullPointerException, SecurityException, or ANR from blocking the main thread). Without these logs, we're guessing, but let's cover the most common culprits.

2. Fix Permission Issues (Super Common!)

You mentioned adding external storage permissions in the Manifest, but that's only half the story for modern Android versions:

  • Static Permissions in Manifest:
    For Android 12 and below, add this:
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
    For Android 13+, replace it with the media-specific permission:
    <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
    
  • Dynamic Permission Request:
    Android 6.0 (API 23) and above require you to ask the user for permission at runtime before accessing external storage. Here's a quick example using the modern ActivityResultContracts:
    // In your Activity's onCreate
    val requestPermissionLauncher = registerForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) { isGranted: Boolean ->
        if (isGranted) {
            // Permission granted, start loading music
            loadMusicList()
        } else {
            // Show error to user
            Toast.makeText(this, "Permission needed to access music", Toast.LENGTH_SHORT).show()
        }
    }
    
    // Call this when your Activity starts or when you need to load music
    if (ContextCompat.checkSelfPermission(
            this,
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                Manifest.permission.READ_MEDIA_AUDIO
            } else {
                Manifest.permission.READ_EXTERNAL_STORAGE
            }
        ) != PackageManager.PERMISSION_GRANTED
    ) {
        requestPermissionLauncher.launch(
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                Manifest.permission.READ_MEDIA_AUDIO
            } else {
                Manifest.permission.READ_EXTERNAL_STORAGE
            }
        )
    } else {
        // Permission already granted
        loadMusicList()
    }
    

3. Stop Blocking the Main Thread!

If you're loading music files directly (like scanning directories) or querying media on the main thread, Android will kill your app for ANR (Application Not Responding). Always do IO operations in a background thread:

  • Use Kotlin Coroutines (recommended):
    private fun loadMusicList() {
        lifecycleScope.launch(Dispatchers.IO) {
            val musicList = queryMusicFiles()
            // Switch back to main thread to update ListView
            withContext(Dispatchers.Main) {
                val adapter = ArrayAdapter(this@YourActivity, android.R.layout.simple_list_item_1, musicList)
                listView.adapter = adapter
            }
        }
    }
    

4. Use MediaStore Instead of Direct File Access (Android Q+)

Starting from Android 10 (API 29), Scoped Storage blocks direct access to external storage directories. Instead, use the MediaStore API to query music files:

private suspend fun queryMusicFiles(): List<String> {
    val musicList = mutableListOf<String>()
    val projection = arrayOf(
        MediaStore.Audio.Media.TITLE,
        MediaStore.Audio.Media.DATA // Note: DATA is deprecated in Android Q, use URI instead if possible
    )
    val selection = "${MediaStore.Audio.Media.IS_MUSIC} != 0"
    val sortOrder = "${MediaStore.Audio.Media.TITLE} ASC"

    contentResolver.query(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        projection,
        selection,
        null,
        sortOrder
    )?.use { cursor ->
        val titleColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)
        val dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)

        while (cursor.moveToNext()) {
            val title = cursor.getString(titleColumn)
            val path = cursor.getString(dataColumn)
            musicList.add("$title - $path")
        }
    }
    return musicList
}

5. Double-Check ListView Setup

Make sure your ListView is correctly initialized in your layout and code:

  • In your XML layout, ensure you have a ListView with an ID (e.g., android:id="@+id/musicListView")
  • In your Activity, initialize it with val listView = findViewById<ListView>(R.id.musicListView) before setting the adapter

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:52:24