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😱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😛adding="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😱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😱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😱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]
 
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:
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.
 

Neue Themen


Zurück
Oben