我是android编程的新手,现在我正在尝试做作业。我在这里阅读了所有类似的主题,但似乎我缺少了一些东西。
我需要使用onTouch播放raw文件夹中的wav声音。
这是我的代码。

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {



    Button bullButton = (Button) findViewById(R.id.button1);
    MediaPlayer mp;

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

        bullButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    mp = MediaPlayer.create(MainActivity.this,R.raw.bull);
                    mp.start();
                }
                else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    mp.stop();
                }

                return true;
            }
        });

    }


}

问题是-每次尝试启动我的应用时都会崩溃。

我在这里有一个崩溃日志:
12/06 17:03:15: Launching app
Cold swapped changes.
$ adb shell am start -n "com.foxysoft.soundplayonclick/com.foxysoft.soundplayonclick.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: com.foxysoft.soundplayonclick.test | com.foxysoft.soundplayonclick
Waiting for application to come online: com.foxysoft.soundplayonclick.test | com.foxysoft.soundplayonclick
Waiting for application to come online: com.foxysoft.soundplayonclick.test | com.foxysoft.soundplayonclick
Connecting to com.foxysoft.soundplayonclick
I/art: Debugger is active
I/System.out: Debugger has connected
I/System.out: waiting for debugger to settle...
Connected to the target VM, address: 'localhost:8601', transport: 'socket'
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1393)
I/InstantRun: Instant Run Runtime started. Android package is com.foxysoft.soundplayonclick, real application class is null.
W/art: Failed to find OatDexFile for DexFile /data/data/com.foxysoft.soundplayonclick/files/instant-run/dex/slice-slice_1-classes.dex ( canonical path /data/data/com.foxysoft.soundplayonclick/files/instant-run/dex/slice-slice_1-classes.dex) with checksum 0x69cf59e0 in OatFile /data/data/com.foxysoft.soundplayonclick/cache/slice-slice_1-classes.dex
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.foxysoft.soundplayonclick, PID: 5533
                  java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.foxysoft.soundplayonclick/com.foxysoft.soundplayonclick.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                      at android.app.ActivityThread.access$800(ActivityThread.java:151)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      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:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                      at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:116)
                      at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
                      at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
                      at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:53)
                      at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:205)
                      at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
                      at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
                      at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:193)
                      at com.foxysoft.soundplayonclick.MainActivity.<init>(MainActivity.java:15)
                      at java.lang.reflect.Constructor.newInstance(Native Method)
                      at java.lang.Class.newInstance(Class.java:1606)
                      at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      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:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

我看了这个话题:
Play sound continuously onTouch()

看起来我也一样,怎么了?

最佳答案

好吧:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.foxysoft.soundplayonclick/com.foxysoft.soundplayonclick.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

并将您的代码更改为此:
Button bullButton;
MediaPlayer mp;

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

    bullButton = (Button) findViewById(R.id.button1); // you need the R.layout.activity_main to be loaded to make a connection to it!
    bullButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                mp = MediaPlayer.create(MainActivity.this,R.raw.bull);
                mp.start();
            }
            else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                mp.stop();
            }

            return true;
        }
    });
}

如果您仍然通过这种方式仍然遇到错误,请与我们共享错误日志。 (格式正确的问题顺便说一句!)

关于java - 尝试在PlaySound onTouch上崩溃。安卓,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40997226/

10-09 04:02