Android App stürzt nach Modifizierung ab

Endymion

Bekanntes Mitglied
Hi, ich habe die GameActivity meiner App verändert, und seitdem stürzt die haupt-Activity beim setzen des OnClickListeners des Buttons ab. Ich poste den Code aus allen Klassen, falls es auch daran liegt:
Java:
package de.androidnewcomer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MueckenfangActivity extends Activity implements OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);
    }
    
    public void onClick(View v) {
    	startActivity(new Intent(this,GameActivity.class));
    }
Java:
package de.androidnewcomer;

import java.util.Date;
import java.util.Random;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GameActivity extends Activity implements Runnable, OnClickListener {
	private static final int INTERVAL = 100;
	private static final int MAX_TIME = 600;
	private static final long MOSQUITO_EXISTING_TIME = 2000;
	private static final String ELEPHANT = "elephant";
	private Random random = new Random();
	private Handler handler = new Handler();
	private FrameLayout gameSpace;
	private boolean running;
	private int gameSpaceWidth;
	private int gameSpaceHeight;
	private int round;
	private int score;
	private int mosquitosNeeded;
	private int mosquitosCatched;
	private int timeLeft;
	private float scale;
	private TextView tvScore;
	private TextView tvRound;
	private TextView tvHits;
	private TextView tvTime;
	private FrameLayout flHits;
	private LayoutParams lpHits;
	private FrameLayout flTime;
	private LayoutParams lpTime;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game);
		gameSpace = (FrameLayout) findViewById(R.id.gameSpace);
		scale = getResources().getDisplayMetrics().density;
		tvScore = (TextView) findViewById(R.id.score);
		tvRound = (TextView) findViewById(R.id.round);
		tvHits = (TextView) findViewById(R.id.hits);
		tvTime = (TextView) findViewById(R.id.time);
		flHits = (FrameLayout) findViewById(R.id.bar_hits);
		lpHits = flHits.getLayoutParams();
		flTime = (FrameLayout) findViewById(R.id.bar_time);
		lpTime = flTime.getLayoutParams();
		startGame();
	}

	private void startGame() {
		round = 0;
		score = 0;
		running = true;
		startRound();
	}

	private void startRound() {
		round += 1;
		mosquitosNeeded = round * 20;
		mosquitosCatched = 0;
		timeLeft = MAX_TIME;
		int id = getResources().getIdentifier(
				"hintergrund" + Integer.toString(round), "drawable",
				this.getPackageName());
		if (id > 0) {
			LinearLayout l = (LinearLayout) findViewById(R.id.background);
			l.setBackgroundResource(id);
		}
		updateScreen();
		handler.postDelayed(this, INTERVAL);
	}

	private boolean testRoundEnd() {
		if (timeLeft == 0) {
			startRound();
			return true;
		}
		return false;
	}

	private boolean testGameEnd() {
		if (timeLeft == 0 && mosquitosCatched < mosquitosNeeded) {
			gameOver();
			return true;
		}
		return false;
	}

	private void gameOver() {
		Dialog dialog = new Dialog(this,
				android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
		dialog.setContentView(R.layout.gameover);
		dialog.show();

	}

	private void tick() {
		if (running) {
			handler.postDelayed(this, INTERVAL);
		}
		timeLeft -= 1;
		float randomNumber = random.nextFloat();
		double probability = mosquitosNeeded * 1.5 / MAX_TIME;
		while (probability > 1) {
			showMosquito();
		}
		if (randomNumber < probability) {
			showMosquito();
		}
		deleteMosquito();
		updateScreen();
		if (!testGameEnd()) {
			testRoundEnd();
		}
	}

	private void updateScreen() {
		tvScore.setText(Integer.toString(score));
		tvRound.setText(Integer.toString(round));
		tvHits.setText(Integer.toString(mosquitosCatched));
		tvTime.setText(Integer.toString(timeLeft));
		lpHits.width = Math
				.round(scale * 300
						* Math.min(mosquitosCatched, mosquitosNeeded)
						/ mosquitosNeeded);
		lpTime.width = Math.round(scale * timeLeft * 300 / MAX_TIME);
	}

	private void showMosquito() {
		gameSpaceWidth = gameSpace.getWidth();
		gameSpaceHeight = gameSpace.getHeight();
		ImageView mosquito = new ImageView(this);
		if (random.nextFloat() < 0.05) {
			mosquito.setImageResource(R.drawable.elephant);
			mosquito.setTag(ELEPHANT);
		} else {
			mosquito.setImageResource(R.drawable.mosquito);
		}
		mosquito.setTag(R.id.geburtsdatum, new Date());
		mosquito.setOnClickListener(this);
		int mosquitoWidth = (int) Math.round(scale * 50);
		int mosquitoHeight = (int) Math.round(scale * 42);
		int links = random.nextInt(gameSpaceWidth - mosquitoWidth);
		int oben = random.nextInt(gameSpaceHeight - mosquitoHeight);
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				mosquitoWidth, mosquitoHeight);
		params.leftMargin = links;
		params.topMargin = oben;
		params.gravity = Gravity.TOP + Gravity.LEFT;
		gameSpace.addView(mosquito, params);
	}

	private void deleteMosquito() {
		gameSpace = (FrameLayout) findViewById(R.id.gameSpace);
		int nummer = 0;
		while (nummer < gameSpace.getChildCount()) {
			ImageView muecke = (ImageView) gameSpace.getChildAt(nummer);
			Date geburtsdatum = (Date) muecke.getTag(R.id.geburtsdatum);
			long alter = (new Date()).getTime() - geburtsdatum.getTime();
			if (alter > MOSQUITO_EXISTING_TIME) {
				gameSpace.removeView(muecke);
			} else {
				nummer++;
			}
		}
	}

	@Override
	public void run() {
		tick();
	}

	@Override
	public void onClick(View mosquito) {
		if (mosquito.getTag(R.id.animal) == ELEPHANT) {
			score -= 1000;
		} else {
			mosquitosCatched++;
			score += 100;
		}
		updateScreen();
		gameSpace.removeView(mosquito);
	}
}
[XML]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:gravity="center"
android:eek:rientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textSize="20sp" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:src="@drawable/mosquito" >
</ImageView>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start" >
</Button>

</LinearLayout>[/XML]
[XML]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:eek:rientation="vertical" >

<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/gelb"
android:textStyle="bold" >
</TextView>

<TextView
android:id="@+id/round"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/blau"
android:textStyle="bold" >
</TextView>
</FrameLayout>

<FrameLayout
android:id="@+id/gameSpace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:layout_weight="1" >
</FrameLayout>

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="0"
android:gravity="bottom"
android:eek:rientation="vertical" >

<FrameLayout
android:id="@+id/frameLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<FrameLayout
android:id="@+id/bar_hits"
android:layout_width="50dip"
android:layout_height="5dip"
android:layout_gravity="center_vertical"
android:background="@color/gruen" >
</FrameLayout>

<TextView
android:id="@+id/hits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/gruen" >
</TextView>
</FrameLayout>

<FrameLayout
android:id="@+id/frameLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
android:id="@+id/bar_time"
android:layout_width="80dp"
android:layout_height="5dp"
android:layout_gravity="center_vertical"
android:background="@color/rot" >
</FrameLayout>

<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/rot" >
</TextView>
</FrameLayout>
</LinearLayout>

</LinearLayout>[/XML]
[XML]<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transgrau"
android:eek:rientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/gameover"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/rot"
android:textStyle="bold" >
</TextView>

</FrameLayout>[/XML]
[XML]<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="gelb">#ffcc00</color>
<color name="blau">#6688ee</color>
<color name="rot">#ee2200</color>
<color name="gruen">#00ff22</color>
<color name="schwarz">#000000</color>
<color name="weiss">#ffffff</color>
<color name="transgelb">#88ffcc00</color>
<color name="transgrau">#88888888</color>
</resources>
[/XML]
[XML]<?xml version="1.0" encoding="utf-8"?>
<resources>

<item name="geburtsdatum" type="id"></item>
<item name="animal" type="id"></item>

</resources>[/XML]
[XML]<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Willkommen beim Mückenfang</string>
<string name="app_name">Mueckenfang</string>
<string name="start">Start</string>
<string name="gameover">Game Over</string>
</resources>
[/XML]
 

eRaaaa

Top Contributor
Du hast etwas entschiedenes vergessen, nämlich die Fehlermeldung! Das Thema hatten wir bei dir doch schon mal, hast du aus dem letzten Mal nichts gelernt? :(

Ich könnte jetzt raten: Du hast die zweite Activity nicht in dem AndroidManifest eingetragen/bekannt gemacht und daher fliegt dir eine ActivityNotFound-Exceptrion um die Ohren.
Trag ins AndroidManifest.xml
[XML] <activity android:name=".GameActivity"></activity>[/XML]
ein
 
Zuletzt bearbeitet:

Endymion

Bekanntes Mitglied
Oh, entschuldigung, hatte ich vergessen. Naja ich habe immer eine NullPointerException in der Zeile, in der der OnClickListener initialisiert wird, bekommen. Ich habe das ganze jetzt einfach mal umgangen, indem ich den OnClickListener einfach mit [XML]android:OnClick="onClick"[/XML] im Layout initialisiert habe, das klappt.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
W App stürzt bei 2. Foto ab Android & Cross-Platform Mobile Apps 3
K App stürzt ab Android & Cross-Platform Mobile Apps 29
I Android Auf ImageView aus einem anderen Thread zugreifen liefert Fehlermeldung (App stürzt ab) Android & Cross-Platform Mobile Apps 5
O Google Admob Ad wird nicht geladen und App stürzt ab Android & Cross-Platform Mobile Apps 1
L App stürzt ab. Android & Cross-Platform Mobile Apps 2
T Android Eigene App stürzt ab Android & Cross-Platform Mobile Apps 3
J Android App stürzt ab wenn neue activity gestartet Android & Cross-Platform Mobile Apps 6
B Android GameLoopThread stürzt beim 2ten start der App ab? Android & Cross-Platform Mobile Apps 4
W getSystemService(SENSOR_SERVICE) - App stürzt ab Android & Cross-Platform Mobile Apps 5
P Android Programm stürzt ab - Es geht um Netzwerk Android & Cross-Platform Mobile Apps 5
J MIDlet installieren - Telefon stürzt ab! Android & Cross-Platform Mobile Apps 3
AllBlack Auf der Suche nach einem App-Entwickler Android & Cross-Platform Mobile Apps 1
J Android zugrif auf Thread nach Handy drehen. Android & Cross-Platform Mobile Apps 10
AGW App schließt nach 2 Sekunden Android & Cross-Platform Mobile Apps 2
ruutaiokwu Android Daten von "Activity A" nach "Activity B" umleiten? Android & Cross-Platform Mobile Apps 13
A Android-Studio: 2. Layout nach kurzer Zeit aufzeigen Android & Cross-Platform Mobile Apps 2
B Profilpic wird nach anmeldung nicht angezeigt. Android & Cross-Platform Mobile Apps 2
J BLOB nach dem Download unbrauchbar Android & Cross-Platform Mobile Apps 0
B App schließt nach Start. Android & Cross-Platform Mobile Apps 12
J Android Nach Appsprachenänderung die Systemsprache ermitteln Android & Cross-Platform Mobile Apps 2
G App wird nach Installation auf Smartphone beendet Android & Cross-Platform Mobile Apps 1
B Android wie kann ich in einer xml nach bestimme item suchen (DOM) Android & Cross-Platform Mobile Apps 7
Fischkralle Android Nach Textdateien in Ordner suchen Android & Cross-Platform Mobile Apps 5
V Android Fehlermeldung beim Öffnen von Eclipse nach Installation der Android Erweiterung Android & Cross-Platform Mobile Apps 4
T Android Nach Buttonclick neu laden Android & Cross-Platform Mobile Apps 3
B Android Activity nach gedrückte Returntaste weiterlaufen lassen Android & Cross-Platform Mobile Apps 2
B Android ringProgressDialog nach Erfolg Button einfärben Android & Cross-Platform Mobile Apps 2
N PriceScannerApp: warum wird nach dem Scannen Display gleich schwarz? Android & Cross-Platform Mobile Apps 4
L Android Bildschirm bleibt dunkel nach neustarten der App nach betätigen des Home-Buttons Android & Cross-Platform Mobile Apps 3
N Android EditText.setError() funktioniert nicht nach Rotation Android & Cross-Platform Mobile Apps 1
B Android Button erstellen nach Vorlage Android & Cross-Platform Mobile Apps 4
L Android Button mit Pfeil nach rechts Android & Cross-Platform Mobile Apps 1
M Android App startet nach Tastensperre neu Android & Cross-Platform Mobile Apps 3
P Android Nach Animation Layout auf alten Platz Android & Cross-Platform Mobile Apps 3
G Werteübergabe nach unbestimmter Zeit Android & Cross-Platform Mobile Apps 28
A Fehlermeldung nach Neuinstallation von Eclipse/bestehenden Projekten... Android & Cross-Platform Mobile Apps 2
N Textview macht immer nach einem Beistrich einen Abstand Android & Cross-Platform Mobile Apps 6
K Apps durchsuchen nach verwendeter Methode Android & Cross-Platform Mobile Apps 4
M Android MediaRecorder - Crash nach 2. Start Android & Cross-Platform Mobile Apps 2
S rms recordstore bleibt nach schießen der anwengung nicht erhalten Android & Cross-Platform Mobile Apps 4
M Text in txt-Datei schreiben und nach ABC sortieren? Android & Cross-Platform Mobile Apps 2
H FileConnection: Frage nach Dateisystem-Zugriff unterdrücken Android & Cross-Platform Mobile Apps 5
K suche nach der richtigen dokumentationh Android & Cross-Platform Mobile Apps 2
S ein String nach vorgegebenen Zeichen teilen Android & Cross-Platform Mobile Apps 3
L Ungültiges Java-Archiv (jar) nach Programmentwicklung Android & Cross-Platform Mobile Apps 4

Ähnliche Java Themen

Neue Themen


Oben