Android onTouchEvent上的空指针异常
我的类onTouchEvent上有一个空指针异常。 这是一个名为Isola的游戏,我们需要用makeMove函数选择一个Rect I checkAndroid onTouchEvent上的空指针异常,android,exception,pointers,null,touch-event,Android,Exception,Pointers,Null,Touch Event,我的类onTouchEvent上有一个空指针异常。 这是一个名为Isola的游戏,我们需要用makeMove函数选择一个Rect I check @Override public boolean onTouchEvent(MotionEvent event) { // determine if we are in single touch or multi touch mode if (event.getPointerCount() == 1) { // for
@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");
}
}
初始化:
// 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);
}
}
谢谢大家! 在哪里初始化这些变量? 触摸,触摸。敏感 您的代码中应该有这样的内容:
touches=new Boolean[n];
touchx=new Integer[n];
touchy=new Integer[n];
我遇到了类似的错误,因为我没有初始化onTouch方法中使用的字符串变量。在您的情况下,您可能已经初始化了数组,但数组项为空,因此它无法将它们与任何值进行比较并给出空指针?您能将第200行指向我们吗?Isola.javaI不明白它从何而来-异常堆栈跟踪告诉您-java.lang.NullPointerException 03-20 19:32:59.467:E/AndroidRuntime17723:at com.example.loup_theron_2866207.Isola.onTouchEventIsola.java:200 Isola.java行200,在onTouchEvent处理程序中。是的,我看到这一行,但我不明白为什么……我怀疑这是正确的代码行。@你仍然在学习完全与你私人布尔接触[];私有float-touchx[];私人浮子敏感[]@user3262670这不是初始化,只是声明。好的,这是变量的声明。。。现在必须初始化它们,因为它们是数组。按照我的建议去做,它应该会起作用;很抱歉是的,我像你一样做了我的:touchs=new boolean[16];touchx=新浮点[16];touchy=新浮动[16];