Ich hab Probleme mit der Programmierung des OnKlickListeners.
Könnt Ihr mir weiterhelfen?
Es soll auf den Käfer geklickt werden damit er dann losrennt. Sobald er nochmal angeklickt wird, soll er wieder stehen bleiben.
Könnt ihr mir weiterhelfen?
Code
package com.example.flava.kaefertest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
final GameView Spiel = new GameView(this);
private ImageView Anzeige1;
private ScheduledExecutorService Exekutor;
private boolean AN = true;
private void executeTask(Runnable runnable){
Exekutor = Executors.newSingleThreadScheduledExecutor();
Exekutor.scheduleWithFixedDelay(runnable, 0, 1000, TimeUnit.MILLISECONDS);
}
final Runnable Aufgabe = new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Spiel.moveObject(R.mipmap.insekt2);
}
});
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Anzeige1 = (ImageView) findViewById(R.id.background);
Spiel.setPosition();
Spiel.Anzeige2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (AN)
executeTask(Aufgabe);
else {
Exekutor.shutdown();
Spiel.killObject(R.mipmap.insekt2x);
}
AN = !AN;
return false;
}
});
}
}
Alles anzeigen
Code
package com.example.flava.kaefertest;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.ImageView;
import java.util.Random;
/**
* Created by flava on 27.03.2016.
*/
public class GameView extends View {
public int xStart, yStart;
private int xZiel, yZiel, xDiff, yDiff, xDisplay, yDisplay;
private double Winkel;
public ImageView Anzeige2;
public GameView(Context context) {
super(context);
}
public void setPosition(){
xDisplay = (int) (getResources().getDisplayMetrics().widthPixels * 0.70);
yDisplay = (int) (getResources().getDisplayMetrics().heightPixels * 0.70);
xStart = xDisplay/2;
yStart = yDisplay/2;
}
public void moveObject(int bild){
setDestination();
getDirection();
Anzeige2.setImageResource(bild);
Anzeige2.setRotation((float) Winkel);
Anzeige2.animate().x(xZiel).y(yZiel).setDuration(1000);
xStart = xZiel;
yStart = yZiel;
}
private void setDestination(){
Random Zufall = new Random();
xZiel = Zufall.nextInt(xDisplay);
yZiel = Zufall.nextInt(yDisplay);
}
private void getDirection(){
xDiff = xZiel - xStart;
yDiff = yZiel - yStart;
Winkel = Math.atan2(xDiff, -yDiff);
Winkel = Math.toDegrees(Winkel);
}
public void killObject(int bild){
Anzeige2.setImageResource(bild);
Anzeige2.animate().alpha(0f).setDuration(1000);
}
}
Alles anzeigen
Code
03-28 09:11:44.821 24214-24214/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.flava.kaefertest, PID: 24214
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.flava.kaefertest/com.example.flava.kaefertest.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3093)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:92)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:537)
at android.view.View.<init>(View.java:4038)
at com.example.flava.kaefertest.GameView.<init>(GameView.java:22)
at com.example.flava.kaefertest.MainActivity.<init>(MainActivity.java:17)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1095)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3083)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Alles anzeigen