I was developing a Music App but whenever I am closing the App, it shows "Unfortunately, Music App has stopped" error. I checked the logcat and this is the cause of the error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
I don't understand why this error is showing because I am closing the app and this line(where the error showed i guess) was not supposed to get executed.
Below are my java files.
MainActivity.java:
public class MainActivity extends AppCompatActivity {
ListView lv;
Button start,next,prev;
MyAdapter myAdapter;
String icon;
int SongPosition;
ArrayList<String> songList= new ArrayList<String>();
ArrayList<String> songPath= new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetFiles(getContentResolver());
lv =(ListView)findViewById(R.id.list);
myAdapter = new MyAdapter();
lv.setAdapter(myAdapter);
icon = "resume";
start = (Button)findViewById(R.id.button);
next = (Button)findViewById(R.id.next);
prev = (Button)findViewById(R.id.prev);
//Pause/Resume playing
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//startService(new Intent(getBaseContext(),MyService.class));
// mynotification();
switch (icon) {
case "resume":
Intent iPause = new Intent(MainActivity.this, MyService.class);
iPause.putExtra("STATUS", 2);
startService(iPause);
icon = "pause";
start.setBackgroundResource(android.R.drawable.ic_media_play);
break;
case "pause":
Intent iResume = new Intent(MainActivity.this,MyService.class);
iResume.putExtra("STATUS",3);
startService(iResume);
icon = "resume";
start.setBackgroundResource(android.R.drawable.ic_media_pause);
break;
default:
Toast.makeText(MainActivity.this, "invalid", Toast.LENGTH_SHORT).show();
}
}
});
//next track
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SongPosition =SongPosition+1;
String nextSongName = songList.get(SongPosition);
String nextSongPath = songPath.get(SongPosition);
Intent nextIntent = new Intent(MainActivity.this,MyService.class);
nextIntent.putExtra("SONG_NAME",nextSongName);
nextIntent.putExtra("SONG_PATH",nextSongPath);
nextIntent.putExtra("STATUS",1);
startService(nextIntent);
}
});
//previous track
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//stopService(new Intent(getBaseContext(),MyService.class));
// mynotification();
SongPosition = SongPosition-1;
String prevSongName = songList.get(SongPosition);
String prevSongPath = songPath.get(SongPosition);
Intent nextIntent = new Intent(MainActivity.this,MyService.class);
nextIntent.putExtra("SONG_NAME",prevSongName);
nextIntent.putExtra("SONG_PATH",prevSongPath);
nextIntent.putExtra("STATUS",1);
startService(nextIntent);
}
});
//display all songs
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String songName = songList.get(position);
SongPosition = position;
String Path = songPath.get(position);
int state = 1;
Intent lvintent = new Intent(MainActivity.this,MyService.class);
lvintent.putExtra("SONG_NAME",songName);
lvintent.putExtra("SONG_PATH",Path);
lvintent.putExtra("STATUS",state);
startService(lvintent);
start.setBackgroundResource(android.R.drawable.ic_media_pause);
}
});
}
public void GetFiles(ContentResolver contentResolver)
{
Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = contentResolver.query(musicUri, null, null, null, null);
if(musicCursor!=null && musicCursor.moveToFirst()) {
//get columns
int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int pathColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.DATA);
//Toast.makeText(this, songPath, Toast.LENGTH_SHORT).show();
while (musicCursor.moveToNext()) {
String title = musicCursor.getString(titleColumn);
String path = musicCursor.getString(pathColumn);
songPath.add(path);
songList.add(title);
}
}
musicCursor.close();
}
public class MyAdapter extends BaseAdapter
{
LayoutInflater mInflater;
public MyAdapter()
{
mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return songList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (convertView==null)
{
v = mInflater.inflate(R.layout.musiclist,null);
}
TextView musicName = (TextView)v.findViewById(R.id.musicName);
musicName.setText(songList.get(position));
return v;
}
}
@Override
protected void onStart() {
Intent intent = new Intent(MainActivity.this,MyService.class);
startService(intent);
start.setBackgroundResource(android.R.drawable.ic_media_play);
super.onStart();
}}
MyService.java
public class MyService extends Service {
String SongName,SongPath;
int getStatus;
private MediaPlayer player;
private int Songpos;
SharedPreferences settings;
SharedPreferences.Editor editor;
public static final String MyPREFERENCES = "MyPrefs" ;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
super.onCreate();
Songpos=0;
player = new MediaPlayer();
// settings = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
// editor = settings.edit();
//String storedName = settings.getString("name","");
// String storedPath = settings.getString("path","");
}
@Override
public int onStartCommand(Intent intent,int flags, int startId) {
SongName = intent.getStringExtra("SONG_NAME");
SongPath = intent.getStringExtra("SONG_PATH");
getStatus=intent.getIntExtra("STATUS",0);
if (getStatus==1) {
player.reset();
try {
player.setDataSource(SongPath);
player.prepare();
player.start();
} catch (Exception e) {
e.printStackTrace();
}
}
else if (getStatus==2)
{
onPause();
}
else if (getStatus==3)
{
onResume();
}
return super.onStartCommand(intent, flags, startId);
}
public void onPause(){
player.pause();
Songpos= player.getCurrentPosition();
}
public void onResume() {
player.seekTo(Songpos);
player.start();
}
@Override
public void onDestroy() {
player.reset();
player.release();
super.onDestroy();
}}