我的音乐播放器应用程序一次又一次地崩溃
Posted
技术标签:
【中文标题】我的音乐播放器应用程序一次又一次地崩溃【英文标题】:My Music Player App is crashing again and again 【发布时间】:2021-10-16 11:58:27 【问题描述】:下面我粘贴了MainActivity的代码
package com.example.tunifi;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;
import java.io.File;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
ListView listView;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
Dexter.withContext(this)
.withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
.withListener(new PermissionListener()
@Override
public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse)
//Toast.makeText(MainActivity.this, "Runtime permission given !", Toast.LENGTH_SHORT).show();
ArrayList<File> mySongs = fetchSongs(Environment.getExternalStorageDirectory());
String [] items = new String[mySongs.size()];
for (int i=0;i<mySongs.size();i++)
items[i] = mySongs.get(i).getName().replace(".mp3","");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1,items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
Intent intent = new Intent(MainActivity.this, PlaySong.class);
String currentSong = listView.getItemAtPosition(position).toString();
intent.putExtra("SongList",mySongs);
intent.putExtra("CurrentSong",currentSong);
intent.putExtra("Position",position);
startActivity(intent);
);
@Override
public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse)
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken)
permissionToken.continuePermissionRequest();
)
.check();
public ArrayList<File> fetchSongs(File file)
ArrayList arrayList = new ArrayList();
File [] songs = file.listFiles();
if (songs != null)
for (File myFile : songs)
if (!myFile.isHidden() && myFile.isDirectory())
arrayList.addAll(fetchSongs(myFile));
else
if (myFile.getName().endsWith(".mp3") && !myFile.getName().startsWith("."))
arrayList.add(myFile);
return arrayList;
下面我贴的是PlaySong的代码
package com.example.tunifi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
public class PlaySong extends AppCompatActivity
TextView textView;
ImageView previous, play, next;
ArrayList<File> songs;
MediaPlayer mediaPlayer;
String textContent;
int position;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_song);
textView = findViewById(R.id.textView);
previous = findViewById(R.id.previous);
play = findViewById(R.id.play);
next = findViewById(R.id.next);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
songs = (ArrayList) bundle.getParcelableArrayList("songs");
textContent = intent.getStringExtra("CurrentSong");
textView.setText(textContent);
position = intent.getIntExtra("Position",0);
Uri uri = Uri.parse(songs.get(position).toString());
mediaPlayer = MediaPlayer.create(this,uri);
mediaPlayer.start();
这是我的 Logcat 显示的内容:
2021-08-13 13:05:25.898 23725-23725/? I/zygote: Not late-enabling -Xcheck:jni (already on)
2021-08-13 13:04:58.820 1538-23594/? I/AudioFlinger: AudioFlinger's thread 0xb1903c80 tid=23594 ready to run
2021-08-13 13:05:25.917 23725-23725/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
2021-08-13 13:03:30.914 1697-2352/system_process W/ActivityManager: Slow operation: 566ms so far, now at getContentProviderImpl: done!
2021-08-13 13:05:25.898 23725-23725/? I/zygote: Not late-enabling -Xcheck:jni (already on)
2021-08-13 13:05:25.917 23725-23725/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
2021-08-13 13:05:25.898 23725-23725/? I/zygote: Not late-enabling -Xcheck:jni (already on)
2021-08-13 13:05:25.917 23725-23725/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
2021-08-13 13:05:26.244 23725-23749/com.example.tunifi D/OpenGLRenderer: HWUI GL Pipeline
2021-08-13 13:05:26.331 23725-23749/com.example.tunifi I/OpenGLRenderer: Initialized EGL, version 1.4
2021-08-13 13:05:26.332 23725-23749/com.example.tunifi D/OpenGLRenderer: Swap behavior 1
2021-08-13 13:05:26.334 23725-23749/com.example.tunifi W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2021-08-13 13:05:26.334 23725-23749/com.example.tunifi D/OpenGLRenderer: Swap behavior 0
2021-08-13 13:05:26.382 23725-23749/com.example.tunifi D/EGL_emulation: eglCreateContext: 0xa0385120: maj 2 min 0 rcv 2
2021-08-13 13:05:26.405 23725-23749/com.example.tunifi D/EGL_emulation: eglMakeCurrent: 0xa0385120: ver 2 0 (tinfo 0xa0383280)
2021-08-13 13:05:26.544 23725-23749/com.example.tunifi D/EGL_emulation: eglMakeCurrent: 0xa0385120: ver 2 0 (tinfo 0xa0383280)
2021-08-13 13:05:28.347 23725-23725/com.example.tunifi D/AndroidRuntime: Shutting down VM
2021-08-13 13:05:28.351 23725-23725/com.example.tunifi E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tunifi, PID: 23725
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.tunifi/com.example.tunifi.PlaySong: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference
at com.example.tunifi.PlaySong.onCreate(PlaySong.java:38)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
有人可以帮我找出问题所在以及如何纠正它,因为我是这个 android 开发世界的新手,我对这件事感到非常沮丧,因为我已经尝试了很多天来纠正它,但没有工作中。非常感谢您!
【问题讨论】:
检查我的编辑@Harsh Gaur 【参考方案1】:诀窍是阅读堆栈跟踪,它说:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference
所以当您尝试获取该列表的值时,songs
看起来为 null,您能否确保在调用之前初始化 songs
Uri uri = Uri.parse(songs.get(position).toString());
编辑
也许这不是你的错误的根本原因,但你正在这样做
intent.putExtra("SongList",mySongs);
看起来您正在使用此捆绑包名称“SongList”发送您的歌曲列表,但随后您尝试使用以下方式检索它:
songs = (ArrayList) bundle.getParcelableArrayList("songs");
我猜应该是“SongList”而不是“songs”
【讨论】:
【参考方案2】:改变
songs = (ArrayList) bundle.getParcelableArrayList("songs");
到
songs = (ArrayList) bundle.getParcelableArrayList("SongList");
【讨论】:
【参考方案3】:
Uri uri = Uri.parse(songs.get(position).toString());
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference
从这个错误中,我们可以知道这个arraylist是空的,你应该先检查一下
【讨论】:
以上是关于我的音乐播放器应用程序一次又一次地崩溃的主要内容,如果未能解决你的问题,请参考以下文章
如何避免要求用户一次又一次地登录(连接)到我的 Facebook 应用程序?