I am coding through Deitel: Android. How to program examples, and in two of them my Android Studio gives a warning / error for anonymous inner classes. He announces that the fragments must be static.
What is the right way to get through this? If I create a static non-anonymous inner class, then there is no warning about the class, but I cannot reference non-static class variables (?). Another way could be to create a separate class (not an inner class), but there is the same problem with variable binding.
This problem is in the Cannon Game example, the CannonView class, the showGameOverDialog method (below), and also on FlagQuiz.
private void showGameOverDialog(final int messageId) {
final DialogFragment gameResult =
new DialogFragment() {
@Override
public Dialog onCreateDialog(Bundle bundle) {
AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity());
builder.setTitle(getResources().getString(messageId));
builder.setMessage(getResources().getString(
R.string.result_format, shotsFired, totalElapsettime
));
builder.setPositiveButton(R.string.reset_game,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialogIsDisplayed = false;
newGame();
}
});
return builder.create();
}
};
activity.runOnUiThread(
new Runnable() {
@Override
public void run() {
showSystemBars();
dialogIsDisplayed = true;
gameResult.setCancelable(false);
gameResult.show(activity.getFragmentManager(), "results");
}
}
);
}
source
share