Deitel How to program Android Cannon Game anonymous class internal warning

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");
                }
            }
    );
}
+4
source share
1 answer
    // display an AlertDialog when the game ends
    private void showGameOverDialog(final int messageId) {
        // DialogFragment to display game stats and start new game
        final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(getResources().getString(messageId));
        // display number of shots fired and total time elapsed
        builder.setMessage(getResources().getString(
                R.string.results_format, shotsFired, totalElapsedTime));
        builder.setPositiveButton(R.string.reset_game,
                new DialogInterface.OnClickListener() {
                    // called when "Reset Game" Button is pressed
                    @Override
                    public void onClick(DialogInterface dialog,
                                        int which) {
                        dialogIsDisplayed = false;
                        newGame(); // set up and start a new game
                    }
                }

        );
        /*       final DialogFragment gameResult =
                new DialogFragment() {
                    // create an AlertDialog and return it
                    @Override
                    public Dialog onCreateDialog(Bundle bundle) {
                        // create dialog displaying String resource for messageId
                        AlertDialog.Builder builder =
                                new AlertDialog.Builder(getActivity());
                        builder.setTitle(getResources().getString(messageId));

                        // display number of shots fired and total time elapsed
                        builder.setMessage(getResources().getString(
                                R.string.results_format, shotsFired, totalElapsedTime));
                        builder.setPositiveButton(R.string.reset_game,
                                new DialogInterface.OnClickListener() {
                                    // called when "Reset Game" Button is pressed
                                    @Override
                                    public void onClick(DialogInterface dialog,
                                                        int which) {
                                        dialogIsDisplayed = false;
                                        newGame(); // set up and start a new game
                                    }
                                }
                        );

                        return builder.create(); // return the AlertDialog
                    }
                };
*/
        // in GUI thread, use FragmentManager to display the DialogFragment
        activity.runOnUiThread(
                new Runnable() {
                    public void run() {
                        final AlertDialog gameResult = builder.create();
                        showSystemBars();
                        dialogIsDisplayed = true;
                        gameResult.setCancelable(false); // modal dialog
                       // gameResult.show(activity.getFragmentManager(), "results");
                        gameResult.show();
                    }
                }
        );
    }
0
source

Source: https://habr.com/ru/post/1694594/


All Articles