I am writing a music player in Java for Android. So far, the app keeps crashing after startup. I'm using Android Studio. I had a look at the 'Logcat' tab at the bottom of the screen and I found the following error message:
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setLooping(boolean)' on a null object reference
at com.example.musicplayer.MainActivity.onCreate(MainActivity.java:37)
Has this anything to do with the app crashing all the time and what can I do to prevent it from crashing?
I tried to run the app in an emulator and a real phone.
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.musicplayer.MainActivity"
android:orientation="vertical"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image"
android:layout_marginTop="60dp"/>
<SeekBar
android:id="@+id/positionBar"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/elapsedTimeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0:11"
android:layout_marginLeft="40dp"/>
<TextView
android:id="@+id/remainingTimeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-1:49"
android:layout_marginLeft="240dp"/>
</LinearLayout>
<Button
android:id="@+id/playBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/play"
android:layout_marginTop="40dp"
android:onClick="playBtnClick"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="40dp"
android:gravity="center">
<ImageView
android:layout_width="18dp"
android:layout_height="18dp"
android:src="@drawable/sound"/>
<SeekBar
android:id="@+id/volumeBar"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:progress="50"
android:max="100"/>
<ImageView
android:layout_width="26dp"
android:layout_height="26dp"
android:src="@drawable/sound2"/>
</LinearLayout>
</LinearLayout>
//MainActivity.java
package com.example.musicplayer;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCo
mpatActivity;
//import android.support.v7.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button playBtn;
SeekBar positionBar;
SeekBar volumeBar;
TextView elapsedTimeLabel;
TextView remainingTimeLabel;
MediaPlayer mp;
int totalTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playBtn = (Button) findViewById(R.id.playBtn);
elapsedTimeLabel = (TextView) findViewById(R.id.elapsedTimeLabel);
//Media Player:
mp = MediaPlayer.create(this, R.raw.music);
mp.setLooping(true);
mp.seekTo(0);
mp.setVolume(0.5f, 0.5f);
totalTime = mp.getDuration();
//Position Bar:
positionBar = (SeekBar) findViewById(R.id.positionBar);
positionBar.setMax(totalTime);
positionBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser){
mp.seekTo(progress);
positionBar.setProgress(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
//Volume Bar:
volumeBar = (SeekBar) findViewById(R.id.volumeBar);
volumeBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float volumeNum = progress / 100f;
mp.setVolume(volumeNum, volumeNum);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
//thread update position bar & time label
new Thread(new Runnable() {
@Override
public void run() {
while (mp != null){
try{
Message msg = new Message();
msg.what = mp.getCurrentPosition();
handler.sendMessage(msg);
Thread.sleep(1000);
}catch (InterruptedException e) {}
}
}
}).start();
}
private Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
int currentPosition = msg.what;
//update position bar
positionBar.setProgress(currentPosition);
//update labels
String elapsedTime = createTimeLabel(currentPosition);
elapsedTimeLabel.setText(elapsedTime);
String remainingTime = createTimeLabel(totalTime-currentPosition);
remainingTimeLabel.setText("- " + remainingTime);
}
};
public String createTimeLabel(int time){
String timeLabel = "";
int min = time / 1000 / 60;
int sec = time / 1000 % 60;
timeLabel = min + ":";
if(sec < 10) timeLabel += "0";
timeLabel += sec;
return timeLabel;
}
public void playBtnClick(View view){
if(!mp.isPlaying()){
//stopping
mp.start();
playBtn.setBackgroundResource(R.drawable.stop);
}else{
//playing
mp.pause();
playBtn.setBackgroundResource(R.drawable.play);
}
}
}
/*I would like the app to start up and keep running so that I can play some music, but instead it closes immediately after I run it. This is the error message I got from Logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setLooping(boolean)' on a null object reference
at com.example.musicplayer.MainActivity.onCreate(MainActivity.java:37)*/