TDS95
Neues Mitglied
- 5
Hallo,
ich versuche zurzeit ein kleines Minesweeper-Spiel zu programmieren.
Doch wenn ich dann versuche die App zu starten stürzt sie direkt mit der Meldung "Leider wurde Minesweeper beendet." ab.
Könnte sich vielleicht mal jemand meinen Source Code angucken und mir sagen was ich falsch gemacht habe?
Mein Source Code:
MinesweeperActivity.java
main.xml
Vielen Dank im Vorraus
ich versuche zurzeit ein kleines Minesweeper-Spiel zu programmieren.
Doch wenn ich dann versuche die App zu starten stürzt sie direkt mit der Meldung "Leider wurde Minesweeper beendet." ab.
Könnte sich vielleicht mal jemand meinen Source Code angucken und mir sagen was ich falsch gemacht habe?
Mein Source Code:
MinesweeperActivity.java
Code:
package com.tds.minesweeper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MinesweeperActivity extends Activity {
/** Called when the activity is first created. */
// @Override
private boolean bomb[];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//single onClickListener
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if (bomb[1]) {
lost();
} else {
button1.setBackgroundColor(Color.RED);
button1.setText("X");
}
}
});
final Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if (bomb[2]) {
lost();
} else {
button2.setBackgroundColor(Color.RED);
button2.setText("X");
}
}
});
final Button button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if (bomb[3]) {
lost();
} else {
button3.setBackgroundColor(Color.RED);
button3.setText("X");
}
}
});
final Button button4 = (Button) findViewById(R.id.button4);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if (bomb[4]) {
lost();
} else {
button4.setBackgroundColor(Color.RED);
button1.setText("X");
}
}
});
final Button button5 = (Button) findViewById(R.id.button5);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if (bomb[5]) {
lost();
} else {
button5.setBackgroundColor(Color.RED);
button1.setText("X");
}
}
});
final Button button6 = (Button) findViewById(R.id.button6);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if (bomb[6]) {
lost();
} else {
button6.setBackgroundColor(Color.RED);
button1.setText("X");
}
}
});
final Button button7 = (Button) findViewById(R.id.button7);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if (bomb[7]) {
lost();
} else {
button7.setBackgroundColor(Color.RED);
button1.setText("X");
}
}
});
final Button button8 = (Button) findViewById(R.id.button8);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if (bomb[8]) {
lost();
} else {
button8.setBackgroundColor(Color.RED);
button1.setText("X");
}
}
});
final Button button9 = (Button) findViewById(R.id.button9);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if (bomb[9]){
lost();
} else
{
button9.setBackgroundColor(Color.RED);
button1.setText("X");
}
}
});
bomb();
};
/*public void onStart(Bundle savedInstanceState) {
super.onStart();
bomb();
}*/
public void bomb() {
int count=0;
for (int i = 1; i <= 9; i++) {
if (count<3 && Math.random()>0.5)
{bomb[i]=true;} else {bomb[i]=false;}
}
// for-Schleife Ende
}
private void lost() {
AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setTitle("Verloren!");
helpBuilder.setMessage("Du hast verloren.");
helpBuilder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
}
});
// Remember, create doesn't show the dialog
AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
}
}
Code:
<?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:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.05" >
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="." />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button5"
android:layout_alignLeft="@+id/button5"
android:text="." />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button2"
android:layout_toLeftOf="@+id/button2"
android:text="." />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_toRightOf="@+id/button5"
android:text="." />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button5"
android:layout_alignBottom="@+id/button5"
android:layout_toLeftOf="@+id/button5"
android:text="." />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_toRightOf="@+id/button5"
android:text="." />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button4"
android:layout_below="@+id/button5"
android:text="." />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button5"
android:layout_below="@+id/button5"
android:text="." />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button6"
android:layout_below="@+id/button6"
android:text="." />
</RelativeLayout>
</LinearLayout>