2

今天我收到了NullPointerException一份报告。我无法在自己的手机上重现此 NullPointerException。

我真的找不到导致 NullPointerException 的原因。

这是报告:

java.lang.NullPointerException
at com.dsf.sdf.ManActivity$25.onTouch(ManActivity.java:445)
at android.view.View.dispatchTouchEvent(View.java:3762)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
at com.android.internal.policy.impl.PhoneWindow$DecorView.
    (PhoneWindow.java:1674)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1109)
at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
at com.android.internal.policy.impl.PhoneWindow$DecorView.
    dispatchTouchEvent(PhoneWindow.java:1658)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.
    run(ZygoteInit.java:876)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
at dalvik.system.NativeStart.main(Native Method)

这是第 936 行的代码:

sdff.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            sdff.setBackgroundDrawable(getResources().
                getDrawable(R.drawable.pressed));
        } 

        else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (play != null) {
                play.reset();
                play.release();
            }
        
            play = MediaPlayer.create(ManActivity.this,
                R.raw.sdff);
            play.start();
            sdff.setBackgroundDrawable(getResources().
                getDrawable(R.drawable.button));
        } 

        else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
            sdff.setBackgroundDrawable(getResources().
                getDrawable(R.drawable.button));
        }
    
        return false;
    }
});

这是一个简单的按钮,按下时会发出声音。

那么是什么导致了异常呢?

编辑:

Code at line 445:

play = MediaPlayer.create(ManActivity.this,
    R.raw.bf);
play.start();
auth.setBackgroundDrawable(getResources().getDrawable(
    R.drawable.button));
4

1 回答 1

1

Rajesh 是对的,@ManActivity.java:445(第 445 行)的代码是什么。如果这是代码,也可以将以下代码用作您的触摸监听器。

    sdff.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                // dont use sdff, use v param that is passed in.
                v.setBackgroundDrawable(getResources()
                        .getDrawable(R.drawable.pressed));
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                if (play != null) {
                    play.reset();
                    play.release();
                }
                play = MediaPlayer.create(ManActivity.this,
                        R.raw.sdff);

                play.start();
                v.setBackgroundDrawable(getResources()
                        .getDrawable(R.drawable.button));
            } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
                v.setBackgroundDrawable(getResources()
                        .getDrawable(R.drawable.button));
            }
            return false;
        }
    });

无论如何,您是否尝试过调试您的应用程序?(右键单击包资源管理器中的 Project > Debug As > Android Application )。然后这将切换到调试视角。找到断点窗格(窗口 > 显示视图 > 断点)。单击 Add JAva execption Breakpoint 按钮(它是一个带有“!”的“J”)。输入“Null”并双击出现的 NullPointerException”。

于 2012-05-07T14:46:46.753 回答