Below i have pasted the code of 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;
}
}
Below i have pasted the code of 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();
}
}
This is what my Logcat shows:
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 ComponentInfo{com.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)
Can someone please help me out in finding what is wrong and how to correct it, sice i am new to this android development world and i am really upset for this thing since i am trying from many days to correct it,but nothing is working. Thank You So Much in advance !
change
songs = (ArrayList) bundle.getParcelableArrayList("songs");
to
songs = (ArrayList) bundle.getParcelableArrayList("SongList");