I am attempting to prompt a user for input based on prior input.
Allow me to clarify and add some context:
This application is a dice-rolling simulator. The main activity displays the number of dice and number of sides on each die, which is changed by opening a sub-activity from the menu. This sub-activity is called for its result using startActivityForResult()
, and consists entirely of a RadioGroup
and two buttons - "Accept" and "Cancel" - and passes a String
extra back to the main activity (in the format "XdN" - e.g. "1d20"), which the main activity then parses and uses to adjust the dice count and side count accordingly.
This system works exceptionally well for examples like "1d6", "1d10", and the like, and is not the problem - as far as I know.
Now, here's my problem:
I have a radio button that passes the String
"Custom" (instead of an "XdN"-like String
). When selected (and, finally, when "Accept" is clicked), my intention is for a dialog to open and prompt the user for custom count values - and this works, sort of. The dialog shows, and the layout is correct and looks like it should, but the layout then disappears after a split-second and passes the current count values instead of the values that would have been entered by the user. This also throws an error to LogCat (I'm using Eclipse and ADT) that says:
Application Tag Text
org.x3chaos.nicedice WindowManager Activity org.x3chaos.nicedice.DiceActivity
has leaked window com.android.internal.pol
icy.impl.PhoneWindow$DecorView@44786f20 th
at was originally added here
org.x3chaos.nicedice WindowManager android.view.WindowLeaked: Activity org.x3
chaos.nicedice.DiceActivity has leaked win
dow com.android.internal.policy.impl.Phone
Window$DecorView@44786f20 that was origina
ly added here
org.x3chaos.nicedice WindowManager at android.view.ViewRoot.<init>(ViewRoot.j
ava:247)
[more if needed]
--
The Button
button_accept's android:onClick
method follows:
public void accept(View view) {
RadioGroup group = (RadioGroup) findViewById(R.id.dice_radiogrp);
RadioButton selected = (RadioButton) findViewById(group
.getCheckedRadioButtonId());
String tmpResult = selected.getText().toString();
if (!tmpResult.matches("[0-9|xX](.*)[Dd][0-9](.*)")) {
// Show dialog
LayoutInflater li = LayoutInflater.from(this);
View dialogView = li.inflate(R.layout.prompt_dice, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
builder.setTitle("Custom Dice");
builder.setCancelable(false);
final EditText editCount = (EditText) findViewById(R.id.edit_promptDiceCount);
final EditText editSides = (EditText) findViewById(R.id.edit_promptDiceSides);
builder.setPositiveButton("Accept", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean flag = false;
String textCount, textSides;
textCount = editCount.getText().toString();
if (textCount.equals("")) {
textCount = "1";
flag = true;
}
textSides = editSides.getText().toString();
if (textSides.equals("")) {
textSides = "20";
flag = true;
}
int count = Integer.parseInt(textCount);
int sides = Integer.parseInt(textSides);
if (count > 20)
count = 20;
if (sides > 20)
sides = 20;
String finRes = count + "d" + sides;
setResultExtra(finRes);
if (flag) {
String msg = "Invalid input: set dice to " + finRes;
Toast toast = Toast.makeText(context, msg,
Toast.LENGTH_SHORT);
toast.show();
}
}
});
builder.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
exitActivity(false);
}
});
AlertDialog dialog = builder.create();
dialog.show();
} else {
result = tmpResult;
}
exitActivity(true);
}
All other functions work, so I'm not sure why this isn't. I'm sure it's something that I'm messing up - seeing as this is my first app and I'm using some objects and methods that I've never touched before - so I really appreciate any help. :]
EDIT: After reading this question, I'm starting to think that maybe it's an Intent
that's not waiting for the dialog to finish. But this doesn't quite make sense, since the activity that calls the dialog is called for a result.
In Android dialogs are asynchronous. You cannot display it and expect that your code waits here. And what you do is dialog.show()
and then instantly exitActivity()
so it seems it works the way you made it.
EDIT
Quick fix for your code could probably be to replace this portion:
} else {
result = tmpResult;
}
exitActivity(true);
with
} else {
result = tmpResult;
exitActivity(true);
}
becasue this exitActivity()
is what causes you problems.