为什么当我使用 MediaPlayer Class 向 运行 音频添​​加方法时应用程序崩溃?

Why the application crashes when I add methods to run audio using MediaPlayer Class?

我已经在资源 "raw" 目录中添加了一个 mp3 文件,以便有一个播放音频文件的按钮和另一个暂停它的按钮,但是当我尝试 运行 它时, 它崩溃了

这是 MainAcitvity class 文件:-

package com.sefonia.mediaplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;


public class MainActivity extends AppCompatActivity {

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

    // Define the Media file player with MediaPlayer Object
    MediaPlayer mediaPlayer= MediaPlayer.create(MainActivity.this,R.raw.mine);

    /**
     * Starts playing the music file
     */
    public void playButton(View view) {
        mediaPlayer.start();
    }

    /**
     * Pauses the music file
     */
    public void pauseButton(View view) {
        mediaPlayer.pause();
    }
}

这就是 xml 关联的文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="28dp"
        android:onClick="playButton"
        android:padding="16sp"
        android:text="Play"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:onClick="pauseButton"
        android:padding="16sp"
        android:text="Pause"
        app:layout_constraintEnd_toEndOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是打印到日志中的消息:

E/ActivityThread: Exception when newActivity r=ActivityRecord{267cffd9 token=android.os.BinderProxy@34033d9e {com.sefonia.mediaplayer/com.sefonia.mediaplayer.MainActivity}} token=android.os.BinderProxy@34033d9e
D/ActivityThread: Dump mRelaunchingActivities:
    Dump mActivities:
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.sefonia.mediaplayer, PID: 13318
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sefonia.mediaplayer/com.sefonia.mediaplayer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2706)
        at android.app.ActivityThread.access0(ActivityThread.java:178)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:6102)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
        at android.content.ContextWrapper.getResources(ContextWrapper.java:86)
        at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
        at androidx.appcompat.app.AppCompatActivity.getResources(AppCompatActivity.java:543)
        at android.media.MediaPlayer.create(MediaPlayer.java:960)
        at android.media.MediaPlayer.create(MediaPlayer.java:943)
        at com.sefonia.mediaplayer.MainActivity.<init>(MainActivity.java:20)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.Class.newInstance(Class.java:1606)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1089)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2508)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2706) 
        at android.app.ActivityThread.access0(ActivityThread.java:178) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:6102) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
Application terminated.

谁能告诉我问题到底出在哪里?

在 onCreate() 方法中初始化媒体播放器对象,您是在 Create() 外部初始化 MediaPlayer 对象。这将起作用,因为您正在使用 MainActivty.this 并且仅在 onCreate() 方法具有 运行 .

之后才创建 Main activity

onCreate() 方法中初始化媒体播放器对象有效

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

        // Define the Media file player with MediaPlayer Object
        mediaPlayer= MediaPlayer.create(MainActivity.this,R.raw.mine);
    }