-2

I am having trouble with an ImageView that I am using which is returning null. The issue seems to be with findViewById and causes a problem when I ateempt to getX() for the imageView. I have tried everything and still can't get it to work. Any help would be greatly appreciated!

xml:

ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/character"
    android:id="@+id/character"
    android:scaleType="fitCenter"
    android:clickable="false"
    android:longClickable="false" /

Java: package fozard.backuptestapp;

public class Play extends AppCompatActivity {

private GameThread thread;
private float charX=0;
ImageView character;

public void setCharacter(ImageView character) {
    this.character = character;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);





}

@Override
protected void onStart(){
    super.onStart();
    thread=new GameThread();
    thread.setRunning(true);
    thread.start();
}

public boolean onTouchEvent(MotionEvent event){
    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            thread.setMovementSpeed(5);
            thread.setMoving(true);
            break;
        case MotionEvent.ACTION_UP:
            thread.setMoving(false);
    }
    return true;
}



public void updateCharPos(float movementSpeed){
    Log.d("Updating", "Should be able to update");
    character = (ImageView)findViewById(R.id.character);
    charX = character.getX();
    System.out.print(charX);
}

}

stack trace:

11-24 09:32:32.918 16706-16706/fozard.backuptestapp D/Updating: Should be able to update
11-24 09:32:32.923 16706-16706/fozard.backuptestapp D/AndroidRuntime: Shutting down VM
11-24 09:32:32.923 16706-16706/fozard.backuptestapp W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41cbc700)
11-24 09:32:32.933 16706-16706/fozard.backuptestapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      java.lang.NullPointerException
                                                                      at fozard.backuptestapp.Play.updateCharPos(Play.java:71)
                                                                      at fozard.backuptestapp.GameThread$1.run(GameThread.java:88)
                                                                      at android.os.Handler.handleCallback(Handler.java:730)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                      at android.os.Looper.loop(Looper.java:176)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                                      at java.lang.reflect.Method.invokeNative(Native Method)
                                                                      at java.lang.reflect.Method.invoke(Method.java:525)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
                                                                      at dalvik.system.NativeStart.main(Native Method)
tripleee
  • 175,061
  • 34
  • 275
  • 318
BFozard
  • 3
  • 3
  • make sure your `ImageView` id has 'character' is inside of `activity_play` layout otherwise it will throw NullPointerExceptoin – Rajesh Nov 24 '16 at 10:08

2 Answers2

0
 ImageButton pauseButton = (ImageButton) findViewById(R.id.pausebutton);

change this

ImageView pauseButton = (ImageView ) findViewById(R.id.character);

you are using wrong id and wrong widget. First read tutorials.

Ramchandra Singh
  • 530
  • 4
  • 15
0

From what i can see in your xml, you use the wrong id for your ImageView, should be:

ImageView pauseButton = (ImageView) findViewById(R.id.character);

and you also have to use the same class for xml and Javacode (ImageView)

DZDomi
  • 1,685
  • 15
  • 13