I am trying to move an ImageView whilst the user touches the screen. I originally tried a while loop from an onTouchEvent method but this halted some background processes that I needed. I now have a thread that I use to control my game which involves a call to update my ImageView's position based on a float called 'movementSpeed' which is determined by Motion events. The issue, I believe, is with the initialization of my ImageView but I have tried many solutions from other questions which don't seem to work for me. I considered whether this would be because I was calling the method from a Thread and tried running the method on the UI thread but it still did not work. However, I have discovered that the position will be updated if I call updateCharPos from my touch event method, although this would cause the aforementioned difficulty in carrying out other tasks.
Activity Java:
package fozard.backuptestapp;
public class Play extends AppCompatActivity {
private GameThread thread;
private float charX=0;
ImageView character;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
character = (ImageView) findViewById(R.id.character);
}
@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");
charX = character.getX();
System.out.print(charX);
}
}
Thread code:
public class GameThread extends Thread {
public static final int maxFps=30;
private double averageFps;
private boolean isRunning;
private int score=0;
private Play play = new Play();
private boolean isMoving;
private float movementSpeed=0;
public void setMovementSpeed(float movementSpeed){
this.movementSpeed = movementSpeed;
}
public void setMoving(Boolean isMoving){
this.isMoving = isMoving;
}
public void setRunning(Boolean isRunning){
this.isRunning = isRunning;
}
@Override
public void run(){
long startTime;
long timeMillis;
long waitTime;
int frameCount=0;
long totalTime=0;
long targetTime= 1000/maxFps;
while (isRunning){
startTime = System.nanoTime();
timeMillis = (System.nanoTime()-startTime)/1000000;
waitTime = targetTime-timeMillis;
try{
if (waitTime>0){
currentThread().sleep(waitTime);
}
}catch (Exception e){
}
totalTime += System.nanoTime() - startTime;
frameCount++;
score=score+1;
if (isMoving){
try{
update();
}catch (Exception e){
e.fillInStackTrace();
}
}
if (frameCount==maxFps){
averageFps=1000/((totalTime/frameCount)/1000000);
frameCount=0;
totalTime=0;
System.out.println(averageFps);
}
}
}
public void update(){
play.runOnUiThread(new Runnable() {
@Override
public void run() {
play.updateCharPos(movementSpeed);
}
});
}
}
and my stack trace:
11-23 19:46:51.715 27465-27504/fozard.backuptestapp I/System.out: 30.0
11-23 19:46:52.710 27465-27504/fozard.backuptestapp I/System.out: 30.0
11-23 19:46:53.705 27465-27504/fozard.backuptestapp I/System.out: 30.0
11-23 19:46:54.105 27465-27465/fozard.backuptestapp D/Updating: Should be able to update
11-23 19:46:54.105 27465-27465/fozard.backuptestapp D/AndroidRuntime: Shutting down VM
11-23 19:46:54.105 27465-27465/fozard.backuptestapp W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41d29700)
11-23 19:46:54.125 27465-27465/fozard.backuptestapp E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1914)
at fozard.backuptestapp.Play.updateCharPos(Play.java:55)
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)
11-23 19:46:54.840 27465-27504/fozard.backuptestapp I/System.out: 30.0
11-23 19:46:55.835 27465-27504/fozard.backuptestapp I/System.out: 30.0
any help would be 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" /