当我单击播放时,应用崩溃

时间:2018-08-18 19:01:21

标签: android

我创建了一个应用程序,当我按下播放按钮时,它应该播放我放入原始文件夹中的MP3文件。但是,当我点击播放时,应用程序便消失了,并且没有错误代码。这是我的Main.Activity.java文件中的代码。我认为问题出在这里。谁能告诉我出什么问题了?-继续-我从这个站点上获得了很多建议,这是我的项目代码的当前外观以及出现的错误。

    package com.games.illusionary.fortnitemusicplayer;


import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

public class MainActivity extends AppCompatActivity {
    MediaPlayer player;
    AdView nAdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        player = MediaPlayer.create(MainActivity.this, R.raw.song);

        MobileAds.initialize(this, "ca-app-pub-9224132391435572~8710572578");
        nAdView = (AdView)findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        nAdView.loadAd(adRequest);
    }








    public void play(View v) {

        stopPlayer();
        player.start();


    }

    public void pause(View v) {
        if (player != null) {
            player.pause();
        }
    }

    public void stop(View v) {
        stopPlayer();
    }

    private void stopPlayer() {
        if (player != null) {
            player.release();
            player = null;
            Toast.makeText(this, "MediaPlayer released", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopPlayer();
    }
}

这是我遇到的错误,它突出显示了player.start,从第42行开始

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
        at com.games.illusionary.fortnitemusicplayer.MainActivity.play(MainActivity.java:42)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
        at android.view.View.performClick(View.java:6597) 
        at android.view.View.performClickInternal(View.java:6574) 
        at android.view.View.access$3100(View.java:778) 
        at android.view.View$PerformClick.run(View.java:25885) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

3 个答案:

答案 0 :(得分:0)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    player = MediaPlayer.create(this, R.raw.song);
}

public void play(View v) {
    if (player != null) { 
        if(player.isPlaying()){
            stopPlayer();
        } else{
                player.start();
        }
    }else{
  /* player returns null, check if        
 player = MediaPlayer.create(this, R.raw.song);
 is not returning null, if it's  returning null issue is with initializing media player */
   Log.e(getClass().toString, "MediaPlayer returns null");
 }
}

答案 1 :(得分:0)

我已经在PC上尝试了您的代码,它也给我带来了错误,我不知道这是什么错误,但是我知道了解决方案,只需使用Activity类而不是AppCompatActivity

package com.games.illusionary.fortnitemusicplayer;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
MediaPlayer player;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void play(View v) {
    if (player == null) {
        player = MediaPlayer.create(this, R.raw.music);
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                stopPlayer();
            }
        });
    }

    player.start();
}

public void pause(View v) {
    if (player != null) {
        player.pause();
    }
}

public void stop(View v) {
    stopPlayer();
}

private void stopPlayer() {
    if (player != null) {
        player.release();
        player = null;
        Toast.makeText(this, "MediaPlayer released", Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onStop() {
    super.onStop();
    stopPlayer();
}

}

答案 2 :(得分:0)

尝试像下面那样全局初始化Mediaplayer对象,然后再次运行您的应用程序:

public class MainActivity extends AppCompatActivity {
    MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        player = Mediaplayer.create(MainActivity.this, R.raw.song);

    }


    public void play(View v){

     stopPlayer();
     player.start();
}

Mediaplayer player;

这将声明一个变量,但不会对其进行初始化(对象引用将为null)。

MediaPlayer.create(this, R.raw.song);

这将创建MediaPlayer实例,但不会将引用存储在任何地方。