I have a onClick() method in another method, and crashes when the gewinnGruen() method is called.
Please help me know why:
public void gewinnGruen() {
Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.winnergreen);
dialog.show();
newgame1 = (Button) findViewById(R.id.newgame1);
newgame1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
field1.setBackgroundResource(R.drawable.field0);
place1 = 0;
}
});
}
Without the onClick() method it doesnt crashes and everything works.
Change this
to
You are hitting NullPointerException in this line.
And this is because, you are trying to initialize a Button in the dialog which you have created, but you didn’t provide the Dialog object to the method here. So it looks into the Activity’s view and returns null, since it can’t find anything of that id there.