Image View Null Pointer Exception

2019-08-26 02:39发布

问题:

I am trying to be able to move the 'giraffe' image view. It seems as though something is wrong with the way im calling the resource from the strings.xml or something it keeps coming back as NullPointerException on the giraffe.setOnTouchListener(this);. Here is the code:

public class MainGameActivity extends Activity implements OnTouchListener {

float x,y,dx,dy;
ImageView giraffe;


@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        ImageView giraffe = (ImageView)findViewById(R.id.giraffe);
        setContentView(R.layout.activity_main_game);
        giraffe.setOnTouchListener(this);
}


public boolean onTouch(View v, MotionEvent me){
    switch(me.getAction()){
        case MotionEvent.ACTION_DOWN:
        {
            x = me.getX();
            y = me.getY();
            dx = x - giraffe.getX();
            dy = y - giraffe.getY();
        }
        break;
        case MotionEvent.ACTION_MOVE :
        {
            giraffe.setX(me.getX()-dx);
            giraffe.setY(me.getY()-dy);
        }
        break;
    }
    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" />

<?xml version="1.0" encoding="utf-8"?>

<string name="app_name">Falling Objects</string>
<string name="menu_settings">Settings</string>
<string name="splash_text">Start dodging in:</string>
<string name="splash_number_countdown" > </string>
<string name="title_activity_main_game" > </string>
<string name="version">Version 1.0</string>
<string name="giraffe">Giraffe</string>
<string name="s_and_b_productions">Created by: S&amp;B Productions</string>
<drawable name="giraffe">#F00</drawable>

    07-05 19:41:52.006: E/AndroidRuntime(1826): FATAL EXCEPTION: main
07-05 19:41:52.006: E/AndroidRuntime(1826): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.SandBProductions.fallingobjects/com.SandBProductions.fallingobjects.MainGameActivity}: java.lang.NullPointerException
07-05 19:41:52.006: E/AndroidRuntime(1826):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-05 19:41:52.006: E/AndroidRuntime(1826):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-05 19:41:52.006: E/AndroidRuntime(1826):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-05 19:41:52.006: E/AndroidRuntime(1826):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-05 19:41:52.006: E/AndroidRuntime(1826):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 19:41:52.006: E/AndroidRuntime(1826):     at android.os.Looper.loop(Looper.java:137)
07-05 19:41:52.006: E/AndroidRuntime(1826):     at android.app.ActivityThread.main(ActivityThread.java:5039)
07-05 19:41:52.006: E/AndroidRuntime(1826):     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 19:41:52.006: E/AndroidRuntime(1826):     at java.lang.reflect.Method.invoke(Method.java:511)
07-05 19:41:52.006: E/AndroidRuntime(1826):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-05 19:41:52.006: E/AndroidRuntime(1826):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-05 19:41:52.006: E/AndroidRuntime(1826):     at dalvik.system.NativeStart.main(Native Method)
07-05 19:41:52.006: E/AndroidRuntime(1826): Caused by: java.lang.NullPointerException
07-05 19:41:52.006: E/AndroidRuntime(1826):     at com.SandBProductions.fallingobjects.MainGameActivity.onCreate(MainGameActivity.java:24)
07-05 19:41:52.006: E/AndroidRuntime(1826):     at android.app.Activity.performCreate(Activity.java:5104)
07-05 19:41:52.006: E/AndroidRuntime(1826):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-05 19:41:52.006: E/AndroidRuntime(1826):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

回答1:

You need to setContentView before findViewById, otherwise the activity doesn't know where to look.



回答2:

this code is wrong:

ImageView giraffe = (ImageView)findViewById(R.id.giraffe);
setContentView(R.layout.activity_main_game);

you have to call SetContentView() prior searching for any layout element. This part should be

setContentView(R.layout.activity_main_game);
ImageView giraffe = (ImageView)findViewById(R.id.giraffe);


回答3:

Try:

setContentView(R.layout.activity_main_game);
ImageView giraffe = (ImageView)findViewById(R.id.giraffe);
        giraffe.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent me){
    switch(me.getAction()){
        case MotionEvent.ACTION_DOWN:
        {
            x = me.getX();
            y = me.getY();
            dx = x - giraffe.getX();
            dy = y - giraffe.getY();
        }
        break;
        case MotionEvent.ACTION_MOVE :
        {
            giraffe.setX(me.getX()-dx);
            giraffe.setY(me.getY()-dy);
        }
        break;
    }
    return true;
}
});

You need to set the content view before you call the Imageview