简体   繁体   中英

Mediaplayer NullPointerException?

I use mediaplayer at my ringtones application (mp3 sounds). It is working correctly but I see error report on google play developer console (for some users),I couldn't find the bug on my device,thank you

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    soundname= new String[]  {"Sound1","Sound2","Sound3","Sound4","Sound5","Sound6"}; 
    soundfile= new int[] {R.raw.sound1,R.raw.sound2,R.raw.sound3,R.raw.sound4,R.raw.sound5,R.raw.sound6};

    this.setContentView(R.layout.single_list_item_view);

    TextView txtProduct = (TextView) findViewById(R.id.product_label);
    Intent intent = getIntent();
    int position = intent.getExtras().getInt("position");

    txtProduct.setText(soundname[position]);
    stopPlaying();  
    mediaPlayer = MediaPlayer.create(this, soundfile[position]); 

    Button btnplay= (Button) findViewById(R.id.btnoynat);
    btnplay.setOnClickListener(new View.OnClickListener(){

        public void onClick(View arg0) { 
                mediaPlayer.start();         
        }
        });
}

private void stopPlaying() {
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;
  }}

Error log; (mediaPlayer.start();-->SingleListItem.java:60)

java.lang.NullPointerException
at com.zilsesleri.SingleListItem$1.onClick(SingleListItem.java:60)  
at android.view.View.performClick(View.java:2538)
at android.view.View$PerformClick.run(View.java:9152)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3693)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
at dalvik.system.NativeStart.main(Native Method)

On line 60 ie mediaplayer.start() the NPE (Null Pointer Exception) is coming because it is finding mediaplayer as null. Hence one way is to put a null option check there

ie

if(mediaplayer != null){
mediaplayer.start();
}

The NPE is coming because creation of mediaplayer from raw sound files is failing on some devices.

Also, you need to stop() and release() mediaplayer in onCompletionListener of MediaPlayer so that it does not fail because of failure of cleaning up resources as mentioned in link:- MediaPlayer->create

eg->

MediaPlayer mediaplayer = MediaPlayer.create(this, R.raw.sound1);        
if(mediaplayer == null) {            
    Log.v(TAG, "Create() on MediaPlayer failed.");       
} else {
    mediaplayer.setOnCompletionListener(new OnCompletionListener() {

    @Override
      public void onCompletion(MediaPlayer mediaplayer) {
          mediaplayer.stop();
          mediaplayer.release();
      }
    });
    mediaplayer.start();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM