When I debug the app it gives me a NullPointerException at the creation of the AlertDialog. I think it is a problem with the Context passed to the AlertDialog.Builder(context) function. Maybe you can help me. Here is the part of the code which contains the error.
Code: Select all
package mskat.android;
import java.util.Arrays;
import java.util.Vector;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
public class Bid extends Activity{
private int bidstep;
private BidStruct bid1;
private BidStruct bid2;
private BidStruct bid3;
private int winner;
public Bid() {
this.bidstep = 0;
StartBidding();
}
private void Player1Says() {
String text = getString(R.string.currentbid_str) + " " + Integer.toString(this.bidstep) + " " + getString(R.string.wantincrease_str);
String highbutton = "";
String lowbutton = "";
if(this.bid1.bid >= this.bidstep) {
highbutton = getString(R.string.yes_str);
lowbutton = getString(R.string.no_str);
}
else {
highbutton = getString(R.string.no_str);
lowbutton = getString(R.string.yes_str);
}
AlertDialog.Builder builder = new AlertDialog.Builder(MobileSkatActivity.context);
builder.setMessage(text);
builder.setCancelable(false);
builder.setTitle(R.string.bid_str);
builder.setPositiveButton(highbutton, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
bidstep = GetBidstep(bidstep);
Player3Answers();
Player1Says();
}
});
builder.setNegativeButton(lowbutton, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private void Player3Answers() {
String text = "";
if(this.bid3.bid >= this.bidstep) {
text = getString(R.string.yes_str);
}
else {
text = getString(R.string.player3_str) + getString(R.string.says_str) + getString(R.string.passe_str);
}
Context context = MobileSkatActivity.context;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
private void Player2Says() {
}
private void Player3Says() {
}
public int StartBidding() {
this.bid1 = GetBid(MobileSkatActivity.deck.GetHand(1));
this.bid2 = GetBid(MobileSkatActivity.deck.GetHand(2));
this.bid3 = GetBid(MobileSkatActivity.deck.GetHand(3));
Player1Says();
return this.winner;
}