我怎样才能删除android中的矩形?

时间:2015-04-13 14:03:49

标签: android

当我尝试删除创建的矩形时......

public boolean onTouchEvent(MotionEvent event) {
     x = event.getX();
     y = event.getY(); 
     if (inside(mx, my)) { }  
     for (int i = 0; i < listRect.size(); i++) { 
         if (listRect.get(i).inside(mx, my))  { 
               index = i; 
         } 
     } 
        Rectangle r = listRect.get(index);
        frame.removeView(r); 
} 

我收到以下错误:

2064-2064/com.shema.example.schema E/InputEventReceiver﹕ Exception dispatching input event.
2064-2064/com.shema.example.schema E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback
2064-2064/com.shema.example.schema E/MessageQueue-JNI﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
            at com.shema.example.schema.Main.onTouchEventMoveRectangle(Main.java:214)
            at com.shema.example.schema.Main.onTouchEvent(Main.java:95)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2742)

1 个答案:

答案 0 :(得分:1)

我认为您还应该删除列表中的矩形。

Rectangle r = listRect.get(index);
frame.removeView(r);
listRect.remove(index);

或者您可以删除列表中的矩形并重绘您的画布(我猜它是画布)

 listRect.remove(index);
 frame.invalidate();