So, musste heute meine Entwicklungsumgebung neu aufsetzen.
Habe das gleichzeitig genutzt um mit dem Spiel nochmal komplett von vorne zu beginnen und habe dementsprechend ein neues Projekt erstellt und das Spiel von Grund auf neu programmiert.
Jetzt komme ich zwar ins Hauptmenü (MainActivity) rein, sobald ich aber eine neues Spiel (GameActivity) starten möchte verabschiedet sich das Spiel mit der Meldung "Leider wurde Minesweeper beendet." (Wir erinnern uns...)
Wenn ich den Logcat richtig verstehe gibt es in Zeile 21 (Rot markiert) eine NullPointer Exception, nur irgendwie finde ich keine, außerdem ist es eigentlich der selbe Code wie vorher und der hat funktioniert...
Logcat:
106-30 22:19:45.989: D/AndroidRuntime(6489): Shutting down VM
206-30 22:19:45.989: W/dalvikvm(6489): threadid=1: thread exiting with uncaught exception (group=0x40a221f8)
306-30 22:19:45.989: E/AndroidRuntime(6489): FATAL EXCEPTION: main
406-30 22:19:45.989: E/AndroidRuntime(6489): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tds.minesweeper/com.tds.minesweeper.GameActivity}: java.lang.NullPointerException
506-30 22:19:45.989: E/AndroidRuntime(6489): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
606-30 22:19:45.989: E/AndroidRuntime(6489): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
706-30 22:19:45.989: E/AndroidRuntime(6489): at android.app.ActivityThread.access$600(ActivityThread.java:132)
806-30 22:19:45.989: E/AndroidRuntime(6489): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
906-30 22:19:45.989: E/AndroidRuntime(6489): at android.os.Handler.dispatchMessage(Handler.java:99)
1006-30 22:19:45.989: E/AndroidRuntime(6489): at android.os.Looper.loop(Looper.java:137)
1106-30 22:19:45.989: E/AndroidRuntime(6489): at android.app.ActivityThread.main(ActivityThread.java:4575)
1206-30 22:19:45.989: E/AndroidRuntime(6489): at java.lang.reflect.Method.invokeNative(Native Method)
1306-30 22:19:45.989: E/AndroidRuntime(6489): at java.lang.reflect.Method.invoke(Method.java:511)
1406-30 22:19:45.989: E/AndroidRuntime(6489): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
1506-30 22:19:45.989: E/AndroidRuntime(6489): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
1606-30 22:19:45.989: E/AndroidRuntime(6489): at dalvik.system.NativeStart.main(Native Method)
1706-30 22:19:45.989: E/AndroidRuntime(6489): Caused by: java.lang.NullPointerException
1806-30 22:19:45.989: E/AndroidRuntime(6489): at com.tds.minesweeper.GameActivity.onCreate(GameActivity.java:21)
1906-30 22:19:45.989: E/AndroidRuntime(6489): at android.app.Activity.performCreate(Activity.java:4465)
2006-30 22:19:45.989: E/AndroidRuntime(6489): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
2106-30 22:19:45.989: E/AndroidRuntime(6489): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
2206-30 22:19:45.989: E/AndroidRuntime(6489): ... 11 more
GameActivity:
[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.Menu;
import android.view.View;
import android.widget.Button;
public class GameActivity extends Activity {
// @Override
private boolean bomb[] = new boolean[25];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button0 = (Button) findViewById(R.id.button0);
button0.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[0] == true) {
lost();
} else {
button0.setBackgroundColor(Color.RED);
button0.setText("X");
}
}
});
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if (bomb[0]) {
lost();
} else {
button1.setBackgroundColor(Color.RED);
button1.setText("X");
}
}
});
final Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[2] == true) {
lost();
} else {
button2.setBackgroundColor(Color.RED);
button2.setText("X");
}
}
});
final Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[3] == true) {
lost();
} else {
button3.setBackgroundColor(Color.RED);
button3.setText("X");
}
}
});
final Button button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[4] == true) {
lost();
} else {
button4.setBackgroundColor(Color.RED);
button4.setText("X");
}
}
});
final Button button5 = (Button) findViewById(R.id.button5);
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[5] == true) {
lost();
} else {
button5.setBackgroundColor(Color.RED);
button5.setText("X");
}
}
});
final Button button6 = (Button) findViewById(R.id.button6);
button6.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[6] == true) {
lost();
} else {
button6.setBackgroundColor(Color.RED);
button6.setText("X");
}
}
});
final Button button7 = (Button) findViewById(R.id.button7);
button7.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[7] == true) {
lost();
} else {
button7.setBackgroundColor(Color.RED);
button7.setText("X");
}
}
});
final Button button8 = (Button) findViewById(R.id.button8);
button8.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[8] == true) {
lost();
} else {
button8.setBackgroundColor(Color.RED);
button8.setText("X");
}
}
});
final Button button9 = (Button) findViewById(R.id.button9);
button9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[9] == true) {
lost();
} else {
button9.setBackgroundColor(Color.RED);
button9.setText("X");
}
}
});
final Button button10 = (Button) findViewById(R.id.button10);
button10.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[10] == true) {
lost();
} else {
button10.setBackgroundColor(Color.RED);
button10.setText("X");
}
}
});
final Button button11 = (Button) findViewById(R.id.button11);
button11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[11] == true) {
lost();
} else {
button11.setBackgroundColor(Color.RED);
button11.setText("X");
}
}
});
final Button button12 = (Button) findViewById(R.id.button12);
button12.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[12] == true) {
lost();
} else {
button12.setBackgroundColor(Color.RED);
button12.setText("X");
}
}
});
final Button button13 = (Button) findViewById(R.id.button13);
button13.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[13] == true) {
lost();
} else {
button13.setBackgroundColor(Color.RED);
button13.setText("X");
}
}
});
final Button button14 = (Button) findViewById(R.id.button14);
button14.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[14] == true) {
lost();
} else {
button14.setBackgroundColor(Color.RED);
button14.setText("X");
}
}
});
final Button button15 = (Button) findViewById(R.id.button15);
button15.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[15] == true) {
lost();
} else {
button15.setBackgroundColor(Color.RED);
button15.setText("X");
}
}
});
final Button button16 = (Button) findViewById(R.id.button16);
button16.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[16] == true) {
lost();
} else {
button16.setBackgroundColor(Color.RED);
button16.setText("X");
}
}
});
final Button button17 = (Button) findViewById(R.id.button17);
button17.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[17] == true) {
lost();
} else {
button17.setBackgroundColor(Color.RED);
button17.setText("X");
}
}
});
final Button button18 = (Button) findViewById(R.id.button18);
button18.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[18] == true) {
lost();
} else {
button18.setBackgroundColor(Color.RED);
button18.setText("X");
}
}
});
final Button button19 = (Button) findViewById(R.id.button19);
button19.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[19] == true) {
lost();
} else {
button19.setBackgroundColor(Color.RED);
button19.setText("X");
}
}
});
final Button button20 = (Button) findViewById(R.id.button20);
button20.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[20] == true) {
lost();
} else {
button20.setBackgroundColor(Color.RED);
button20.setText("X");
}
}
});
final Button button21 = (Button) findViewById(R.id.button21);
button21.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[21] == true) {
lost();
} else {
button21.setBackgroundColor(Color.RED);
button21.setText("X");
}
}
});
final Button button22 = (Button) findViewById(R.id.button22);
button22.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[22] == true) {
lost();
} else {
button22.setBackgroundColor(Color.RED);
button22.setText("X");
}
}
});
final Button button23 = (Button) findViewById(R.id.button23);
button23.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[23] == true) {
lost();
} else {
button23.setBackgroundColor(Color.RED);
button23.setText("X");
}
}
});
final Button button24 = (Button) findViewById(R.id.button24);
button24.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bomb[24] == true) {
lost();
} else {
button24.setBackgroundColor(Color.RED);
button24.setText("X");
}
}
});
bomb();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void bomb() {
int count = 0;
for(int i = 0; i <= 25; i++) {
if (count < 5 && Math.random() > 0.5) {
bomb[i] = true;
count++;
} else {
bomb[i] = false;
}
}
}
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]
MainActivity:
1package com.tds.minesweeper;
2
3import android.app.Activity;
4import android.content.Intent;
5import android.os.Bundle;
6import android.view.Menu;
7import android.view.View;
8
9public class MainActivity extends Activity {
10
11 @Override
12 public void onCreate(Bundle savedInstanceState) {
13 super.onCreate(savedInstanceState);
14 setContentView(R.layout.activity_main);
15 }
16
17 @Override
18 public boolean onCreateOptionsMenu(Menu menu) {
19 getMenuInflater().inflate(R.menu.activity_main, menu);
20 return true;
21 }
22
23 public void startGame(View v) {
24 Intent gameIntent = new Intent(this, GameActivity.class);
25 startActivity(gameIntent);
26 }
27
28 public void close(View v) {
29 this.finish();
30 }
31
32
33}
Hat jemand eine Idee?
Vielen Dank im Vorraus
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.