Это MainActivity. java. Возникает какая-то ошибка после выбора песни из списка. Песня не воспроизводится
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.IOException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static MediaPlayer mp = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView audioView = findViewById(R.id.songlist);
ArrayList<String> audioList = new ArrayList<>();
String[] project = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DISPLAY_NAME };
final Cursor audioCursor =
getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, project, null, null, null);
if(audioCursor != null){
if(audioCursor.moveToFirst()){
do{
int audioIndex =
audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
audioList.add(audioCursor.getString(audioIndex));
}while(audioCursor.moveToNext());
}
}
assert audioCursor != null;
audioCursor.close();
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,android.R.id.text1, audioList);
audioView.setAdapter(adapter);
audioView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
String currentFile = (String) view.getTag();
int cursorPosition =audioCursor.getPosition();
mp.stop();
mp.reset();
if (!currentFile.equals("")){
audioCursor.move(cursorPosition+1);
try {
mp.setDataSource(currentFile);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
}
Это просто начало, я просто хочу получить музыку c играет при выборе песни из списка просмотра. listview работает нормально, хотя при выборе нужной песни она не воспроизводится. Если вам нужна дополнительная информация, дайте мне знать.
2020-02-23 19:28:16.499 31981-31981/? E/e.myapplicatio: Unknown bits set in runtime_flags: 0x28000
2020-02-23 19:28:16.712 31981-32012/? E/Perf: Fail to get file list com.example.myapplication
2020-02-23 19:28:16.713 31981-32012/? E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-02-23 19:28:16.713 31981-32012/? E/Perf: Fail to get file list com.example.myapplication
2020-02-23 19:28:16.713 31981-32012/? E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-02-23 19:28:18.956 31981-31981/com.example.myapplication E/MediaPlayerNative: stop called in state 1, mPlayer(0x0)
2020-02-23 19:28:18.956 31981-31981/com.example.myapplication E/MediaPlayerNative: error (-38, 0)
2020-02-23 19:28:18.961 31981-31981/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 31981
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.example.myapplication.MainActivity$1.onItemClick(MainActivity.java:57)
at android.widget.AdapterView.performItemClick(AdapterView.java:352)
at android.widget.AbsListView.performItemClick(AbsListView.java:1243)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3258)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:228)
at android.app.ActivityThread.main(ActivityThread.java:7772)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)