C
cle1000
Neues Mitglied
- 0
Hallo,
hoffentlich könnt ihr mir weiter helfen, ich versuche gerade tictactoe zu programmieren. es hat auch schon alles geklappt, bis ich einen eigenen button nämlich TicTacToeButton erzeugt habe, den ich anstelle von dem normalen Button verwenden möchte.
Aber leider finde ich den grund für den Fehler nicht
also hier mein code:
TicTacToeActivity
TicTacToeButton
main.xml
Die NullPointerException tritt an dieser Stelle auf:
buttons[j].setOnClickListener(this);
Ich hoffe ihr könnt mir helfen!
lg Clemens
hoffentlich könnt ihr mir weiter helfen, ich versuche gerade tictactoe zu programmieren. es hat auch schon alles geklappt, bis ich einen eigenen button nämlich TicTacToeButton erzeugt habe, den ich anstelle von dem normalen Button verwenden möchte.
Aber leider finde ich den grund für den Fehler nicht
also hier mein code:
TicTacToeActivity
Code:
package at.cbrunner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class TicTacToeActivity extends Activity implements OnClickListener {
TicTacToeButton[][] buttons = new TicTacToeButton[3][3];
int counterX = 0;
int counterO = 0;
boolean isX = true;
TextView textViewStatus;
Button buttonRestart;
Button buttonReset;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setButtons();
textViewStatus = (TextView) findViewById(R.id.textViewStatus);
setStatus();
setContentView(R.layout.main);
}
private void setStatus() {
textViewStatus.setText("X: " + counterX + " ... O:" + counterO);
}
private void setButtons() {
buttons[0][0] = (TicTacToeButton) findViewById(R.id.button1_1);
buttons[0][1] = (TicTacToeButton) findViewById(R.id.button1_2);
buttons[0][2] = (TicTacToeButton) findViewById(R.id.button1_3);
buttons[1][0] = (TicTacToeButton) findViewById(R.id.button2_1);
buttons[1][1] = (TicTacToeButton) findViewById(R.id.button2_2);
buttons[1][2] = (TicTacToeButton) findViewById(R.id.button2_3);
buttons[2][0] = (TicTacToeButton) findViewById(R.id.button3_1);
buttons[2][1] = (TicTacToeButton) findViewById(R.id.button3_2);
buttons[2][2] = (TicTacToeButton) findViewById(R.id.button3_3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j].setOnClickListener(this);
}
}
buttonReset = (Button) findViewById(R.id.buttonReset);
buttonReset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
counterX = 0;
counterO = 0;
setStatus();
resetAll();
}
});
buttonRestart = (Button) findViewById(R.id.buttonRestart);
buttonRestart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
resetAll();
}
});
}
@Override
public void onClick(View view) {
((Button) view).setEnabled(false);
if (isX) {
((TicTacToeButton) view).setX(true);
isX = false;
} else {
((TicTacToeButton) view).setO(true);
isX = true;
}
if (check()) {
setButtonsEnable(false);
setStatus();
}
}
private void resetAll() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j].reset();
}
}
}
private void setButtonsEnable(boolean b) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j].setEnabled(b);
}
}
}
public boolean check() {
//TODO
return false;
}
}
Code:
package at.cbrunner;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.Button;
public class TicTacToeButton extends Button{
private boolean x;
private boolean o;
private Drawable drawableX;
private Drawable drawableO;
private Drawable drawableButton;
public TicTacToeButton(Context context) {
super(context);
init();
}
public TicTacToeButton (Context c, AttributeSet a) {
super(c, a);
init();
}
public TicTacToeButton (Context c, AttributeSet a, int i) {
super(c, a, i);
init();
}
public void init(){
x = false;
o = false;
drawableO = (Drawable)getResources().getDrawable(R.drawable.button_o);
drawableX = (Drawable)getResources().getDrawable(R.drawable.button_x);
drawableButton = (Drawable)getResources().getDrawable(R.drawable.button);
this.setBackgroundDrawable(drawableButton);
}
public void reset(){
this.setBackgroundDrawable(drawableButton);
this.setEnabled(true);
}
public boolean isX() {
return x;
}
public void setX(boolean x) {
this.x = x;
if (x){
this.setBackgroundDrawable(drawableX);
this.setEnabled(false);
}
}
public boolean isO() {
return o;
}
public void setO(boolean o) {
this.o = o;
if (o){
this.setBackgroundDrawable(drawableO);
this.setEnabled(false);
}
}
}
Code:
02-28 21:53:05.195: I/Process(304): Sending signal. PID: 304 SIG: 9
02-28 21:53:09.017: D/AndroidRuntime(326): Shutting down VM
02-28 21:53:09.075: W/dalvikvm(326): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
02-28 21:53:09.075: E/AndroidRuntime(326): Uncaught handler: thread main exiting due to uncaught exception
02-28 21:53:09.075: E/AndroidRuntime(326): java.lang.RuntimeException: Unable to start activity ComponentInfo{at.cbrunner/at.cbrunner.TicTacToeActivity}: java.lang.NullPointerException
02-28 21:53:09.075: E/AndroidRuntime(326): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-28 21:53:09.075: E/AndroidRuntime(326): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-28 21:53:09.075: E/AndroidRuntime(326): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-28 21:53:09.075: E/AndroidRuntime(326): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-28 21:53:09.075: E/AndroidRuntime(326): at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 21:53:09.075: E/AndroidRuntime(326): at android.os.Looper.loop(Looper.java:123)
02-28 21:53:09.075: E/AndroidRuntime(326): at android.app.ActivityThread.main(ActivityThread.java:4363)
02-28 21:53:09.075: E/AndroidRuntime(326): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 21:53:09.075: E/AndroidRuntime(326): at java.lang.reflect.Method.invoke(Method.java:521)
02-28 21:53:09.075: E/AndroidRuntime(326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-28 21:53:09.075: E/AndroidRuntime(326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-28 21:53:09.075: E/AndroidRuntime(326): at dalvik.system.NativeStart.main(Native Method)
02-28 21:53:09.075: E/AndroidRuntime(326): Caused by: java.lang.NullPointerException
02-28 21:53:09.075: E/AndroidRuntime(326): at at.cbrunner.TicTacToeActivity.setButtons(TicTacToeActivity.java:51)
02-28 21:53:09.075: E/AndroidRuntime(326): at at.cbrunner.TicTacToeActivity.onCreate(TicTacToeActivity.java:24)
02-28 21:53:09.075: E/AndroidRuntime(326): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-28 21:53:09.075: E/AndroidRuntime(326): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
02-28 21:53:09.075: E/AndroidRuntime(326): ... 11 more
02-28 21:53:09.105: I/dalvikvm(326): threadid=7: reacting to signal 3
02-28 21:53:09.105: I/dalvikvm(326): Wrote stack trace to '/data/anr/traces.txt'
buttons[j].setOnClickListener(this);
Ich hoffe ihr könnt mir helfen!
lg Clemens