描述:
我创建了使用画布绘制矩形的视图,并在该视图中实现了 On Touch Event。
我希望这种接触能够启动新的活动。
问题:
现在的问题是每当我触摸矩形“强制关闭”错误时就会弹出。
这是 PickCardView.java
public class PickCardView extends View
{
private Paint mCanvasPaint;
private Rect mScreenSize = new Rect();
private Rect mCardSize = new Rect();
private int mCardCap;
private Card MCards;
public PickCardView(Context context)
{
super(context);
mCanvasPaint = new Paint();
mCanvasPaint.setColor(0xFF228B22); // Green background
mCanvasPaint.setAntiAlias(false);
mCanvasPaint.setFilterBitmap(false);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
// Store current screen size
mScreenSize.set(0, 0, w, h);
// Calculate card and decks sizes and positions
int cw = w / 11;
mCardSize.set(0, 0, cw, (int) (cw * 1.5));
Log.v("card size", mCardSize.toString());
int freeSize = w - cw * 7; //117
mCardCap = freeSize / (6 + 4 * 2);//5
int cy = (int) (mScreenSize.height() * 0.35);
//MIDDLE CARD HOLDER
MCards = new Card(Card.CardType.PickDeck, mCardCap * 2 + mCardSize.width()*4 - 15, cy - 20, mCardSize.width()+60, mCardSize.height()+60);
}
@Override
public void onDraw(Canvas canvas)
{
mCanvasPaint.setStyle(Style.FILL);
canvas.drawRect(mScreenSize, mCanvasPaint);
MCards.doDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN)
{
int x = (int) event.getX();
int y = (int) event.getY();
Card card = getCardUnderTouch(x, y);
if(card != null)
{
Intent i = new Intent(null, BuildWord.class);
startActivity(i);
}
}
return true;
}
private void startActivity(Intent i)
{
//Intent i1 = new Intent(null, BuildWord.class);
}
private Card getCardUnderTouch(int x, int y)
{
Card ret = null;
if (MCards.isUnderTouch(x, y))
{
return MCards;
}
return ret;
}
}
当我触摸该矩形时,这是 logcat 中显示的错误:
11-13 21:07:12.037: E/InputEventReceiver(712): Exception dispatching input event.
11-13 21:07:12.037: E/MessageQueue-JNI(712): Exception in MessageQueue callback: handleReceiveCallback
11-13 21:07:12.138: E/MessageQueue-JNI(712): java.lang.NullPointerException
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.content.ComponentName.<init>(ComponentName.java:75)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.content.Intent.<init>(Intent.java:3301)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at apk.gameshufflewords.PickCardView.onTouchEvent(PickCardView.java:79)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.view.View.dispatchTouchEvent(View.java:7127)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.view.View.dispatchPointerEvent(View.java:7307)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3174)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3119)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4155)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4134)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4226)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.os.MessageQueue.nativePollOnce(Native Method)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.os.MessageQueue.next(MessageQueue.java:125)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.os.Looper.loop(Looper.java:124)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at java.lang.reflect.Method.invoke(Method.java:511)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 21:07:12.138: E/MessageQueue-JNI(712): at dalvik.system.NativeStart.main(Native Method)
11-13 21:07:12.167: D/AndroidRuntime(712): Shutting down VM
11-13 21:07:12.167: W/dalvikvm(712): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-13 21:07:12.267: E/AndroidRuntime(712): FATAL EXCEPTION: main
11-13 21:07:12.267: E/AndroidRuntime(712): java.lang.NullPointerException
11-13 21:07:12.267: E/AndroidRuntime(712): at android.content.ComponentName.<init>(ComponentName.java:75)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.content.Intent.<init>(Intent.java:3301)
11-13 21:07:12.267: E/AndroidRuntime(712): at apk.gameshufflewords.PickCardView.onTouchEvent(PickCardView.java:79)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.view.View.dispatchTouchEvent(View.java:7127)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
11-13 21:07:12.267: E/AndroidRuntime(712): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
11-13 21:07:12.267: E/AndroidRuntime(712): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
11-13 21:07:12.267: E/AndroidRuntime(712): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.view.View.dispatchPointerEvent(View.java:7307)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3174)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3119)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4155)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4134)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4226)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.os.MessageQueue.nativePollOnce(Native Method)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.os.MessageQueue.next(MessageQueue.java:125)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.os.Looper.loop(Looper.java:124)
11-13 21:07:12.267: E/AndroidRuntime(712): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-13 21:07:12.267: E/AndroidRuntime(712): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 21:07:12.267: E/AndroidRuntime(712): at java.lang.reflect.Method.invoke(Method.java:511)
11-13 21:07:12.267: E/AndroidRuntime(712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-13 21:07:12.267: E/AndroidRuntime(712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-13 21:07:12.267: E/AndroidRuntime(712): at dalvik.system.NativeStart.main(Native Method)