「giraffe.getX()」で Null ポインター例外が発生し、理由がわからない場合は、何か間違ったことを宣言している必要がありますか? 以下は xml のコードとスタック トレースで、エラーは 36 行目にあります dx = x-giraffe.getX()
public class MainGameActivity extends Activity implements OnTouchListener {
float x,y,dx,dy;
ImageView giraffe;
private static final String ONTOUCH_TAG = "Entered onTouch()";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main_game);
ImageView giraffe = (ImageView)findViewById(R.id.giraffe);
giraffe.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent me){
if(me.getAction() == MotionEvent.ACTION_DOWN){
Log.d(ONTOUCH_TAG, "ONTOUCH");
x = me.getX();
y = me.getY();
dx = x - giraffe.getX();
dy = y - giraffe.getY();
}
if(me.getAction() == MotionEvent.ACTION_MOVE)
{
giraffe.setX(me.getX()-dx);
giraffe.setY(me.getY()-dy);
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main_game, menu);
return true;
}
}
レイアウト ファイル:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainGameActivity" >
<ImageView
android:id="@+id/giraffe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="58dp"
android:contentDescription="@string/giraffe"
android:src="@drawable/giraffeicon" />
</RelativeLayout>
スタックトレース
07-07 14:47:49.606: E/AndroidRuntime(1229): FATAL EXCEPTION: main
07-07 14:47:49.606: E/AndroidRuntime(1229): java.lang.NullPointerException
07-07 14:47:49.606: E/AndroidRuntime(1229): at com.SandBProductions.fallingobjects.MainGameActivity.onTouch(MainGameActivity.java:36)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.View.dispatchTouchEvent(View.java:7234)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
07-07 14:47:49.606: E/AndroidRuntime(1229): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
07-07 14:47:49.606: E/AndroidRuntime(1229): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
07-07 14:47:49.606: E/AndroidRuntime(1229): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.View.dispatchPointerEvent(View.java:7419)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.os.MessageQueue.nativePollOnce(Native Method)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.os.MessageQueue.next(MessageQueue.java:125)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.os.Looper.loop(Looper.java:124)
07-07 14:47:49.606: E/AndroidRuntime(1229): at android.app.ActivityThread.main(ActivityThread.java:5039)
07-07 14:47:49.606: E/AndroidRuntime(1229): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 14:47:49.606: E/AndroidRuntime(1229): at java.lang.reflect.Method.invoke(Method.java:511)
07-07 14:47:49.606: E/AndroidRuntime(1229): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-07 14:47:49.606: E/AndroidRuntime(1229): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-07 14:47:49.606: E/AndroidRuntime(1229): at dalvik.system.NativeStart.main(Native Method)