Android:尝试在空对象引用

时间:2015-06-16 02:12:56

标签: java android nullpointerexception

我尝试在我的应用中的某个视图上的其中一个线性布局上设置手势检测。问题是每当我用一个手势测试它时我的应用程序崩溃了。我收到的错误消息是臭名昭着的"尝试在空对象引用上调用虚方法"。我已经完成了研究,我理解这个问题源于尝试在一个Activity上调用方法,而这些方法没有在一个代码中正确地传递到必要的范围内。话虽如此,我不明白如何在我的代码中解决这个问题。以下是相关的代码:

Playfrets.java

public class PlayFrets extends Activity {
    LinearLayout gestureOverlay; 
    GestureListener gestureListener;        
    public void configGestureRecognition(){       //Toolbar gesture recognition
            gestureOverlay = (LinearLayout) findViewById(R.id.toolbarGestureOverlay);
            gestureListener = new GestureListener(PlayFrets.this);
            if (gestureOverlay == null){
                Toast.makeText(PlayFrets.this, "gestureOverlay object is null bro" ,Toast.LENGTH_SHORT).show();
            }
            gestureOverlay.setOnTouchListener(gestureListener);
        }
}

LoadNoteFiles.java

public class LoadNoteFiles extends AsyncTask<Void, Void, Void>{
private Context mContext;
Activity instance;  

public LoadNoteFiles(Context context){ //constructor
    mContext = context;
}

@Override
protected void onPreExecute(){
    PlayFrets.viewSwitch = new ViewSwitcher(mContext);
    PlayFrets.viewSwitch.addView(ViewSwitcher.inflate(mContext, R.layout.splash_screen, null));
    instance = (Activity)mContext;  //cast context from main activity into an activity to access setContentView method
    instance.setContentView(PlayFrets.viewSwitch);
}
...//do in background code omitted
@Override
protected void onPostExecute(Void v){
    PlayFrets.viewSwitch.addView(ViewSwitcher.inflate(mContext, R.layout.activity_play_frets, null));
    PlayFrets.viewSwitch.showNext();
    ((PlayFrets)mContext).configFretboard();
    ((PlayFrets)mContext).configGestureRecognition(); //test gesture recognition
}

GestureListener.java

class GestureListener extends SimpleOnGestureListener implements OnTouchListener {
    Context mContext;
    GestureDetector gDetector;
    static final double SWIPE_MIN_DISTANCE = PlayFrets.getActualHeight()*(0.80);  //must swipe across at least 80% of screen height to register a gesture
    static final int SWIPE_MAX_OFF_PATH = 150;
    static final int SWIPE_THRESHOLD_VELOCITY = 300;

    public GestureListener(Context context) {
        this.mContext = context;
    }

    public GestureListener(Context c, AttributeSet attrs) {
        super();
    }

    public GestureListener(Context context, GestureDetector gDetector) {
        if (gDetector == null){
            gDetector = new GestureDetector(context, this);
        }
        this.mContext = context;
        this.gDetector = gDetector;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
        //Log.e("gesture", String.valueOf(SWIPE_MIN_DISTANCE));
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
            if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH|| Math.abs(velocityY) < SWIPE_THRESHOLD_VELOCITY) {
                return false;
            }
            else if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE) {
                //float traveled = e1.getY() - e2.getY();
                //Toast.makeText(context, Float.toString(traveled) + ">" +String.valueOf(SWIPE_MIN_DISTANCE),Toast.LENGTH_SHORT).show();
                Toast.makeText(mContext, "Swiped Up",Toast.LENGTH_SHORT).show();
            } 
            else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE) {
                //float traveled = e2.getY() - e1.getY();
                //Toast.makeText(context, Float.toString(traveled) + ">" +String.valueOf(SWIPE_MIN_DISTANCE),Toast.LENGTH_SHORT).show();
                Toast.makeText(mContext, "Swiped Down",Toast.LENGTH_SHORT).show();
            }
        } 
        return super.onFling(e1, e2, velocityX, velocityY);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gDetector.onTouchEvent(event);
    }

    public GestureDetector getDetector() {
        return gDetector;
    }

}

我是否需要为GestureListener.java使用不同的构造函数来正确传入活动的实例?究竟是什么导致在null对象上调用onTouch()方法?下面是详细说明错误的日志。

日志文件

06-15 21:59:46.721: E/InputEventReceiver(6972): Exception dispatching input event.
06-15 21:59:46.721: E/MessageQueue-JNI(6972): Exception in MessageQueue callback: handleReceiveCallback
06-15 21:59:46.741: E/MessageQueue-JNI(6972): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.GestureDetector.onTouchEvent(android.view.MotionEvent)' on a null object reference
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at com.example.guitarsim.GestureListener.onTouch(GestureListener.java:59)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.View.dispatchTouchEvent(View.java:8914)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2673)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2385)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2684)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2346)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2684)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2346)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2684)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2346)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2684)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2346)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2684)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2346)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2552)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1767)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.app.Activity.dispatchTouchEvent(Activity.java:2840)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2513)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.View.dispatchPointerEvent(View.java:9125)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4636)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4474)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4024)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4077)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4043)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4157)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4051)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4214)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4024)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4077)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4043)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4051)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4024)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6485)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6384)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6355)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6575)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.os.MessageQueue.nativePollOnce(Native Method)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.os.MessageQueue.next(MessageQueue.java:143)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.os.Looper.loop(Looper.java:130)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at android.app.ActivityThread.main(ActivityThread.java:5835)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at java.lang.reflect.Method.invoke(Native Method)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at java.lang.reflect.Method.invoke(Method.java:372)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
06-15 21:59:46.741: E/MessageQueue-JNI(6972):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

1 个答案:

答案 0 :(得分:4)

学习阅读logcat,它告诉你究竟是什么问题......

mysql

问题发生在06-15 21:59:46.741: E/MessageQueue-JNI(6972): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.GestureDetector.onTouchEvent(android.view.MotionEvent)' on a null object reference 06-15 21:59:46.741: E/MessageQueue-JNI(6972): at com.example.guitarsim.GestureListener.onTouch(GestureListener.java:59) 方法中GestureListener课程的第59行...

onTouch(...)

基本上,您的@Override public boolean onTouch(View v, MotionEvent event) { return gDetector.onTouchEvent(event); } 必须为null,并且尝试调用其gDetector方法导致NPE。

问题在于您在onTouchEvent(...)方法GestureListener中创建Activity的实例,如下所示......

configGestureRecognition()

构造函数gestureListener = new GestureListener(PlayFrets.this); 的版本不会初始化GestureListener(Context context)

使用以GestureDetector作为参数的构造函数...

GestureDetector

...并传入已初始化的public GestureListener(Context context, GestureDetector gDetector) {...} GestureDetector,以便为您创建null