0

'giraffe.getX()' 上的空指针异常,不知道为什么,我一定是在声明错误?下面是代码 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)
4

2 回答 2

3

ImageView 长颈鹿为空:

ImageView giraffe;



@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        giraffe = (ImageView)findViewById(R.id.giraffe);

}
于 2013-07-07T14:56:50.503 回答
3

您已经定义了 giraffe 两次(一次是全局的,一次是在 onCreate 中):

ImageView 长颈鹿 = (ImageView)findViewById(R.id.giraffe);

从此语句中删除 ImageView ,它将起作用。

于 2013-07-07T14:57:20.390 回答