【问题标题】:Null Pointer Exception onTouchEvent空指针异常 onTouchEvent
【发布时间】:2014-04-27 22:40:46
【问题描述】:

我的类 onTouchEvent 上有一个空指针异常。 这是一个名为 Isola 的游戏,我们需要选择一个 Rect(我使用 makeMove() 函数检查它的 rect。)

@Override
public boolean onTouchEvent(MotionEvent event) {
    // determine if we are in single touch or multi touch mode

    if (event.getPointerCount() == 1) {
        // for all events take a copy of the data. on the down press

            if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {

-> 190      touches[0] = true;
            touchx[0] = event.getX();
            touchy[0] = event.getY();
            makeMove();


        } else if (event.getActionMasked() == MotionEvent.ACTION_UP) {
            touches[0] = false;
        } 
        }

        // we need to invalidate the display and return true to indicate
        // that we
        // have handled the event and we need an update
        invalidate();
        return true;
    }
    // we have not handled the event so use the standard event handling



    return super.onTouchEvent(event);
    }

我知道什么是空指针异常,但在这种情况下,我不明白它来自哪里,这里是 LogCat:

    03-20 20:37:53.002: E/InputEventReceiver(27540): Exception dispatching input event.
03-20 20:37:53.002: E/MessageQueue-JNI(27540): Exception in MessageQueue callback: handleReceiveCallback
03-20 20:37:53.012: E/MessageQueue-JNI(27540): java.lang.NullPointerException
03-20 20:37:53.012: E/MessageQueue-JNI(27540):  at com.example.loup_theron_2866207.Isola.onTouchEvent(Isola.java:190)
03-20 20:37:53.012: E/MessageQueue-JNI(27540):  at android.view.View.dispatchTouchEvent(View.java:7706)
03-20 20:37:53.012: E/MessageQueue-JNI(27540):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)

makeMove 方法:

public void makeMove(){
    try{
    for(int j=0;j<7;j++){
        for(int i=0;i<7;i++){
            if(touchx[0] < tableR[j][i] && touchx[0] > tableL[j][i] && touchy[0] < tableB[j][i] && touchy[0] > tableT[j][i]){
                if(tableCheck[j][i] == true){
                    return;
                }
                else if(tableCheck[j][i] == false){
                    tableCheck[j][i] = true;

                }
            }
        }
    }
    }
    catch(Exception e){
        System.out.println("Null pointer 3");
    }
}

init() 的调用:

// default constructor for the class
public Isola(Context context) {
    super(context);
    // call the shared init method
    init();
}

// alternative constructor for the class that takes in two arguments
public Isola(Context context, AttributeSet attr) {
    super(context, attr);
    // call the shared init method
    init();
}

// alternative constructor for the class that takes in three arguments
public Isola(Context context, AttributeSet attr, int defStyle) {
    super(context, attr, defStyle);
    // call the shared init method
    init();
}

初始化:

private void init() {
    // create a few paint objects for drawing up to three different colours
    // for our squares
try {
    btn_bottom.setOnClickListener(this);
    red = new Paint(Paint.ANTI_ALIAS_FLAG);
    green = new Paint(Paint.ANTI_ALIAS_FLAG);
    black = new Paint(Paint.ANTI_ALIAS_FLAG);
    white = new Paint(Paint.ANTI_ALIAS_FLAG);
    red.setColor(0xFFFF0000);
    green.setColor(0xFF00FF00);
    black.setColor(0x00000000);
    white.setColor(0xFFFFFFFF);
    white.setStyle(Paint.Style.FILL_AND_STROKE);

    // initialise all the touch arrays to have just 3 elements as we will
    // only try three
    // touch for now
    touches = new boolean[16];
    touchx = new float[16];
    touchy = new float[16];
    /*
    for(int j=0;j<7;j++){
        for(int i=0;i<7;i++){
            tableL[j][i] = 0;
            tableT[j][i] = 0;
            tableR[j][i] = 0;
            tableB[j][i] = 0;
            tableCheck[j][i] = false;

            }
        }
    */
    // initialise a single square that will be shown at all times
    //touchx[0] = 200;
    //touchy[0] = 200;
    // initialise the rectangle
    //squareSelect = new Rect();
    //squareSelect = new Rect(-75, -75, 75, 75);

    square.setStrokeWidth(3);
    square.setStyle(Paint.Style.STROKE);
    square.setColor(Color.BLACK);

    squareC.setStrokeWidth(3);
    squareC.setStyle(Paint.Style.FILL);
    squareC.setColor(Color.BLACK);
}
catch(Exception e){
System.out.println("Null pointer 2" + e);
}
}

谢谢!

【问题讨论】:

  • 你能把 200 行指向我们吗? (Isola.java)
  • I don't understand where it comes from - 异常堆栈跟踪告诉您确切的位置 - java.lang.NullPointerException 03-20 19:32:59.467: E/AndroidRuntime(17723): at com.example.loup_theron_2866207.Isola.onTouchEvent(Isola.java:200) Isola.java 第 200 行,在 onTouchEvent 处理程序中。
  • 是的,我看到了这条线,但不明白为什么..
  • 我怀疑那行代码是否正确。
  • @still_learning 与你共事

标签: android exception pointers null touch-event


【解决方案1】:

你在哪里初始化这些变量? 触摸,触摸x。敏感吗?

你的代码中应该有这样的东西:

touches=new Boolean[n];
touchx=new Integer[n];
touchy=new Integer[n];

【讨论】:

  • 私有布尔触摸[];私人浮动 touchx[];私人浮动敏感[];
  • @user3262670 这不是初始化,只是声明。
  • 好的,这就是你的变量的声明......现在你必须初始化它们,因为它们是数组。按照我的建议做,它应该可以工作;)
  • 对不起...是的,我像你一样做到了
  • 我的:touches = new boolean[16]; touchx = 新浮点数[16]; touchy = new float[16];
【解决方案2】:

我遇到的错误与我意识到我没有初始化我在 onTouch 方法中使用的字符串变量类似。在您的情况下,您可能已经初始化了数组,但数组项为空,因此无法将它们与任何值进行比较并给出空指针?

【讨论】:

    猜你喜欢
    • 2015-07-12
    相关资源
    最近更新 更多