Android studio 为什么我的应用程序在按下按钮后使用MediaPlayer会崩溃?

Android studio 为什么我的应用程序在按下按钮后使用MediaPlayer会崩溃?,android-studio,debugging,Android Studio,Debugging,这是我关于堆栈溢出的第一篇文章。所以,我的应用程序在我按下一个按钮后一直停止,这个按钮应该会播放审查噪音。它只是崩溃了。 我试着寻找各种各样的解决方案,但什么都没有出现 package com.example.soundbutton; import androidx.appcompat.app.AppCompatActivity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.Vi

这是我关于堆栈溢出的第一篇文章。所以,我的应用程序在我按下一个按钮后一直停止,这个按钮应该会播放审查噪音。它只是崩溃了。 我试着寻找各种各样的解决方案,但什么都没有出现

package com.example.soundbutton;

import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity{
    Button bleep;

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

       bleep = (Button) findViewById(R.id.bleep);

        final MediaPlayer bleepMP = MediaPlayer.create(this, R.raw.censor);

        bleep.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View V) {
                    bleepMP.start();
                }
            });
    }

}
下面是运行完和emulator后的信息。我仍然不太确定我到底在看什么。从我所能推断出的,似乎我指向了某个空值。我尝试将mp值初始化为null,并设置一个条件,如果它为null,那么它将启动onclick

Here is the information after running in an emulator                                    `code`11/20 14:20:48: Launching 'app' on Pixel XL API 29.
$ adb shell am start -n "com.example.soundbutton/com.example.soundbutton.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Waiting for process to come online...
Connected to process 21565 on device 'emulator-5554'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/ple.soundbutto: Not late-enabling -Xcheck:jni (already on)
E/ple.soundbutto: Unknown bits set in runtime_flags: 0x8000
W/ple.soundbutto: Unexpected CPU variant for X86 using defaults: x86
D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
W/RenderThread: type=1400 audit(0.0:53): avc: denied { write } for name="property_service" dev="tmpfs" ino=7228 scontext=u:r:untrusted_app:s0:c136,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 app=com.example.soundbutton
D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
W/ple.soundbutto: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
    Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
E/MediaPlayerNative: error (1, -2147483648)
D/MediaPlayer: create failed:
    java.io.IOException: Prepare failed.: status=0x1
        at android.media.MediaPlayer._prepare(Native Method)
        at android.media.MediaPlayer.prepare(MediaPlayer.java:1274)
        at android.media.MediaPlayer.create(MediaPlayer.java:977)
        at android.media.MediaPlayer.create(MediaPlayer.java:948)
        at com.example.soundbutton.MainActivity.onCreate(MainActivity.java:20)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
D/HostConnection: HostConnection::get() New Host Connection established 0xd9bd3fa0, tid 21629
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_1 
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
D/EGL_emulation: eglCreateContext: 0xe51d4400: maj 3 min 1 rcv 4
D/EGL_emulation: eglMakeCurrent: 0xe51d4400: ver 3 1 (tinfo 0xf04c02e0)
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    glUtilsParamSize: unknow param 0x000082da
W/Gralloc3: mapper 3.x is not supported
D/HostConnection: createUnique: call
D/HostConnection: HostConnection::get() New Host Connection established 0xd9bd5800, tid 21629
    HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_1 
D/eglCodecCommon: allocate: Ask for block of size 0x1000
D/eglCodecCommon: allocate: ioctl allocate returned offset 0x3ff1dc000 size 0x2000
D/EGL_emulation: eglMakeCurrent: 0xe51d4400: ver 3 1 (tinfo 0xf04c02e0)
D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 1 0
I/Choreographer: Skipped 43 frames!  The application may be doing too much work on its main thread.
W/MediaPlayer-JNI: MediaPlayer finalized without being released
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.soundbutton, PID: 21565
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
        at com.example.soundbutton.MainActivity$1.onClick(MainActivity.java:25)
        at android.view.View.performClick(View.java:7125)
        at android.view.View.performClickInternal(View.java:7102)
        at android.view.View.access$3500(View.java:801)
        at android.view.View$PerformClick.run(View.java:27336)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Process 21565 terminated.

请提供崩溃日志。以下是在模拟器中运行后的信息,堆栈跟踪显示
bleepMP
onClick()
中为空。显然,您已经尝试在上面对其进行初始化,这表明
MediaPlayer.create()
方法返回null,这意味着它在尝试加载该资源时失败。您确定
censor
文件没有损坏,并且是
MediaPlayer
支持的格式吗?是的,@MikeM。mp3文件已损坏,谢谢!