我/我有一个 NullPointerException,我相信这个问题与 onCreate 方法中的 setContentView 有关。
这是主要活动:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
这是我将文本添加到 textView 的代码。这段代码是在另一个视图中编写的:
public boolean onTouchEvent(MotionEvent event) {
// get pointer index from the event object
int pointerIndex = event.getActionIndex();
// get pointer ID
int pointerId = event.getPointerId(pointerIndex);
// get masked (not specific to a pointer) action
int maskedAction = event.getActionMasked();
switch (maskedAction) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN: {
// We have a new pointer. Lets add it to the list of pointers
PointF f = new PointF();
f.x = event.getX(pointerIndex);
f.y = event.getY(pointerIndex);
mActivePointers.put(pointerId, f);
break;
}
case MotionEvent.ACTION_MOVE: { // a pointer was moved
for (int size = event.getPointerCount(), i = 0; i < size; i++) {
PointF point = mActivePointers.get(event.getPointerId(i));
if (point != null) {
point.x = event.getX(i);
point.y = event.getY(i);
}
}
break;
}
case MotionEvent.ACTION_UP:
String schema = getLetterSchema();
String letter = getLetter(schema);
if(letter == null) {
System.out.println("***************************");
System.out.println("****ERROR***"+schema+"***");
System.out.println("***************************");
} else {
TextView t=(TextView)findViewById(R.id.textView);
t.setText(letter);
System.out.println("***************************");
System.out.println("*"+letter+"*");
System.out.println("****************"+schema+"****");
}
mActivePointers.clear();
break;
case MotionEvent.ACTION_POINTER_UP:
// DO NOT USE
break;
case MotionEvent.ACTION_CANCEL: {
// DO NOT USE
mActivePointers.clear();
break;
}
}
;
invalidate();
return true;
}
这是日志:
09-09 18:01:47.221: I/(29842): PLATFORM VERSION : JB-MR-2
09-09 18:01:47.246: D/mali_winsys(29842): new_window_surface returns 0x3000
09-09 18:01:47.321: D/OpenGLRenderer(29842): Enabling debug mode 0
09-09 18:01:50.546: E/InputEventReceiver(29842): Exception dispatching input event.
09-09 18:01:50.551: E/MessageQueue-JNI(29842): Exception in MessageQueue callback: handleReceiveCallback
09-09 18:01:50.556: E/MessageQueue-JNI(29842): java.lang.NullPointerException
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at com.example.multitouch.MultitouchView.onTouchEvent(MultitouchView.java:127)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.View.dispatchTouchEvent(View.java:8112)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2420)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2153)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2420)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2153)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2420)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2153)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2420)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2153)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2252)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1587)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.app.Activity.dispatchTouchEvent(Activity.java:2507)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2200)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.View.dispatchPointerEvent(View.java:8320)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4558)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4430)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3988)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4042)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4011)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4122)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4019)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4179)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3988)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4042)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4011)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4019)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3988)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6333)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6249)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6220)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6185)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6413)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.os.MessageQueue.nativePollOnce(Native Method)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.os.MessageQueue.next(MessageQueue.java:138)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.os.Looper.loop(Looper.java:123)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at android.app.ActivityThread.main(ActivityThread.java:5335)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at java.lang.reflect.Method.invoke(Method.java:515)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
09-09 18:01:50.556: E/MessageQueue-JNI(29842): at dalvik.system.NativeStart.main(Native Method)
09-09 18:01:50.556: D/AndroidRuntime(29842): Shutting down VM
09-09 18:01:50.556: W/dalvikvm(29842): threadid=1: thread exiting with uncaught exception (group=0x4175fc08)
09-09 18:01:50.571: E/AndroidRuntime(29842): FATAL EXCEPTION: main
09-09 18:01:50.571: E/AndroidRuntime(29842): Process: com.example.multitouch, PID: 29842
09-09 18:01:50.571: E/AndroidRuntime(29842): java.lang.NullPointerException
09-09 18:01:50.571: E/AndroidRuntime(29842): at com.example.multitouch.MultitouchView.onTouchEvent(MultitouchView.java:127)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.View.dispatchTouchEvent(View.java:8112)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2420)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2153)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2420)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2153)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2420)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2153)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2420)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2153)
09-09 18:01:50.571: E/AndroidRuntime(29842): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2252)
09-09 18:01:50.571: E/AndroidRuntime(29842): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1587)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.app.Activity.dispatchTouchEvent(Activity.java:2507)
09-09 18:01:50.571: E/AndroidRuntime(29842): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2200)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.View.dispatchPointerEvent(View.java:8320)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4558)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4430)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3988)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4042)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4011)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4122)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4019)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4179)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3988)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4042)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4011)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4019)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3988)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6333)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6249)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6220)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6185)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6413)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.os.MessageQueue.nativePollOnce(Native Method)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.os.MessageQueue.next(MessageQueue.java:138)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.os.Looper.loop(Looper.java:123)
09-09 18:01:50.571: E/AndroidRuntime(29842): at android.app.ActivityThread.main(ActivityThread.java:5335)
09-09 18:01:50.571: E/AndroidRuntime(29842): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 18:01:50.571: E/AndroidRuntime(29842): at java.lang.reflect.Method.invoke(Method.java:515)
09-09 18:01:50.571: E/AndroidRuntime(29842): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
09-09 18:01:50.571: E/AndroidRuntime(29842): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
09-09 18:01:50.571: E/AndroidRuntime(29842): at dalvik.system.NativeStart.main(Native Method)
09-09 18:01:53.381: I/Process(29842): Sending signal. PID: 29842 SIG: 9
第 127 行是这样的:t.setText(letter);
这是主要活动:
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}