When generating a signature in Android Studio, an error is displayed:

Error: should be static (com.example.goshany.myapplicationATimerPDA.MainActivityATimerPDA.ByebyeDialogFragment) [ValidFragment]

on code:

public class ByebyeDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builderout = new AlertDialog.Builder(getActivity()); builderout.setMessage(R.string.exitTimer) .setPositiveButton(R.string.Yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { finish();} }); builderout.setNegativeButton(R.string.No, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }); return builderout.create(); } } 

If the class is static, the finish() method is underlined in red. This happens when Build type-release . How can I fix it?

    1 answer 1

    Try this:

     getActivity().finish(); 

    I highly recommend the use of non-static internal fragments, but if you really want to, you can shut up lint

     android { ... lintOptions { abortOnError false checkReleaseBuilds false } } 

    It is worth remembering that you may not see other potential errors that the medium could tell you about. Moreover, if you use the libraries of support for version 25, this fragment will fall.

    • Option getActivity (). Finish (); does not pass. - Gosha
    • I apologize for the hasty comment. GetActivity (). Finish (); works with the static class. - Gosha