Hi I'm having problems with an android app I'm building. It is a mediaplayer that allows playing at the background. It keeps crashing when I try to run it with the mentioned exception. I would really appreciate some help on how to stop this error please. Here's my Main Activity:
package com.example.katelee.mp3player;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.katelee.mp3player.myService.LocalBinder;
import android.net.Uri;
import android.os.IBinder;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private Intent i;
myService mService;
boolean mBound = false;
private Button btnPlayPause, btnStop;
private SeekBar seekBar;
MP3Player mediaPlayer;
private TextView tx;
public String selectedMusic;
File musicDir = new File(Environment.getExternalStorageDirectory().getPath()+ "/Music/");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPlayPause = (Button) findViewById(R.id.play);
btnStop = (Button) findViewById(R.id.stop);
final ListView lv = (ListView) findViewById(R.id.list_view);
seekBar = (SeekBar) findViewById(R.id.seek_bar);
tx = (TextView) findViewById(R.id.textView);
mediaPlayer = MP3Player.getInstance();
btnPlayPause.setOnClickListener(new OnClickPlayPauseListener());
btnStop.setOnClickListener(new OnClickStopListener());
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt,long mylng) {
File selectedFromList =(File) (lv.getItemAtPosition(myItemInt));
Log.d("g53mdp", selectedFromList.getAbsolutePath());
selectedMusic = selectedFromList.getAbsolutePath();
}
});
File list[] = musicDir.listFiles();
lv.setAdapter(new ArrayAdapter<File>(this,android.R.layout.simple_list_item_1, list));
}
@Override
protected void onStart() {
super.onStart();
// Bind to LocalService
i = new Intent(this, myService.class);
bindService(i, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
private class OnClickPlayPauseListener implements OnClickListener {
@Override
public void onClick(View arg0) {
if(mBound)
{
if(mService.getState() == MP3Player.MP_STATE.PLAYING) {
mService.pause();
}
else if (mService.getState() == MP3Player.MP_STATE.PAUSED) {
mService.start();
}
else {
seekBar = (SeekBar) findViewById(R.id.seek_bar);
configureSeekBar(seekBar);
Uri uri = Uri.parse(selectedMusic);
mService.playLocalFile(getApplicationContext(), uri);
}
}
}
}
private class OnClickStopListener implements OnClickListener {
@Override
public void onClick(View v) {
mService.stop();
}
}
/** Defines callback for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
public void configureSeekBar(SeekBar seekBar) {
this.seekBar = seekBar;
new Thread(new SeekBarThread()).start();
}
private class SeekBarThread implements Runnable {
@Override
public void run() {
try {
while (mService.getState().equals(MP3Player.MP_STATE.STOPPED)) {
Thread.sleep(1000);
}
seekBar.setMax(mService.getDuration());
seekBar.setOnSeekBarChangeListener(new MyOnSeekBarChangedListener());
while (true) {
if (mService.getState() != MP3Player.MP_STATE.STOPPED) {
if(mService.getCurrentPosition() < mService.getDuration()){
seekBar.setProgress(mService.getCurrentPosition());
}
}
else {
seekBar.setProgress(0);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
private class MyOnSeekBarChangedListener implements OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
if(fromTouch) {
mService.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//Empty method
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//Empty method
}
}
}
And this is my service class:
package com.example.katelee.mp3player;
import com.example.katelee.mp3player.MP3Player.MP_STATE;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
public class myService extends Service implements OnCompletionListener {
private MP3Player mediaPlayer = MP3Player.getInstance();
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
myService getService() {
return myService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
}
public synchronized MP_STATE getState() {
return mediaPlayer.getState();
}
public void start() {
mediaPlayer.start();
}
public void pause() {
mediaPlayer.pause();
}
public void stop() {
mediaPlayer.stop();
}
public void playLocalFile(Context c, Uri filepath) {
mediaPlayer.playLocalFile(c, filepath);
}
public int getDuration() {
return mediaPlayer.getDuration();
}
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
public void seekTo(int sec)
{
mediaPlayer.seekTo(sec);
}
}
My MP3Player class:
package com.example.katelee.mp3player;
import java.io.IOException;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
public class MP3Player extends MediaPlayer {
private static MP3Player myMediaPlayer;
private MP_STATE state;
public enum MP_STATE {
ERROR,
PLAYING,
PAUSED,
STOPPED
}
public static MP3Player getInstance() {
if (myMediaPlayer == null) {
myMediaPlayer = new MP3Player();
}
return myMediaPlayer;
}
private MP3Player() {
state = MP3Player.MP_STATE.STOPPED;
}
@Override
public void start() {
state = MP_STATE.PLAYING;
}
@Override
public void pause() {
state = MP_STATE.PAUSED;
}
@Override
public void stop() {
state = MP_STATE.STOPPED;
super.stop();
super.reset();
}
public void playLocalFile(Context c, Uri filepath) {
if (!state.equals(MP_STATE.STOPPED)) {
stop();
}
setAudioStreamType(AudioManager.STREAM_MUSIC);
setOnPreparedListener(new MyOnPreparedListener());
try {
setDataSource(c, filepath);
prepareAsync();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public synchronized MP_STATE getState() {
return state;
}
private class MyOnPreparedListener implements OnPreparedListener {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
}
public int getCurrentPosition() {
return super.getCurrentPosition();
}
public int getDuration()
{
return super.getDuration();
}
public void seekTo(int sec){
super.seekTo(sec);
}
}
Here's the logcat:
12-08 01:03:48.488 13915-13915/? I/art: Not late-enabling -Xcheck:jni (already on)
12-08 01:03:48.519 13915-13921/? E/art: Failed sending reply to debugger: Broken pipe
12-08 01:03:48.520 13915-13921/? I/art: Debugger is no longer active
12-08 01:03:48.554 13915-13915/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.katelee.mp3player-1/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.katelee.mp3player-1@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
12-08 01:03:48.671 13915-13915/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.katelee.mp3player-1/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.katelee.mp3player-1@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
12-08 01:03:48.692 13915-13915/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.katelee.mp3player-1/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.katelee.mp3player-1@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
12-08 01:03:48.736 13915-13915/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.katelee.mp3player-1/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.katelee.mp3player-1@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
12-08 01:03:48.758 13915-13915/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.katelee.mp3player-1/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.katelee.mp3player-1@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
12-08 01:03:48.782 13915-13915/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.katelee.mp3player-1/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.katelee.mp3player-1@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
12-08 01:03:48.810 13915-13915/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.katelee.mp3player-1/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.katelee.mp3player-1@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
12-08 01:03:48.832 13915-13915/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.katelee.mp3player-1/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.katelee.mp3player-1@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
12-08 01:03:48.850 13915-13915/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.katelee.mp3player-1/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.katelee.mp3player-1@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
12-08 01:03:48.873 13915-13915/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.katelee.mp3player-1/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.katelee.mp3player-1@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
12-08 01:03:48.911 13915-13915/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.katelee.mp3player-1/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.example.katelee.mp3player-1@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
12-08 01:03:48.912 13915-13915/? W/System: ClassLoader referenced unknown path: /data/app/com.example.katelee.mp3player-1/lib/x86
12-08 01:03:48.914 13915-13915/? I/InstantRun: Starting Instant Run Server for com.example.katelee.mp3player
12-08 01:03:49.413 13915-13915/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-08 01:03:49.911 13915-13915/com.example.katelee.mp3player D/AndroidRuntime: Shutting down VM
12-08 01:03:49.913 13915-13915/com.example.katelee.mp3player E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.katelee.mp3player, PID: 13915
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.katelee.mp3player/com.example.katelee.mp3player.MainActivity}: java.lang.NullPointerException: storage == null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: storage == null
at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
at java.util.Arrays.asList(Arrays.java:155)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:137)
at com.example.katelee.mp3player.MainActivity.onCreate(MainActivity.java:67)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I've already included the necessary tag in my Manifest file so the issue should rise from there. I know the problem that my storage is null but I'm pretty sure it is not null so I'm not sure how to tackle this problem :(