gpt4 book ai didi

java - 当我添加使用 MediaPlayer 类运行音频的方法时,为什么应用程序崩溃?

转载 作者:行者123 更新时间:2023-12-02 01:23:38 26 4
gpt4 key购买 nike

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

这是 MainAcitvity 类文件:-

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.access$800(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.access$800(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 。

关于java - 当我添加使用 MediaPlayer 类运行音频的方法时,为什么应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57600476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com