Home » Android » android – onCreateDialog not annotated method overrides method annotated with @NonNull

android – onCreateDialog not annotated method overrides method annotated with @NonNull

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am creating DialogFragment and when I want to override onCreateDialog I receive the following warning:

not annotated method overrides method annotated with @NonNull

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return super.onCreateDialog(savedInstanceState);
}

If I want to place that annotation to my method, Android Studio can’t find that annotation.

Why is this happening? Thanks for your help.

How to&Answers:

Because you override a method which is defined with a @NonNull annotation (meaning the method must not return null), and you are not using the same annotation in your overridden implementation, so that makes it a mismatch.

Please search for your question before submitting, this is asked many times.

Meaning of Android Studio error: Not annotated parameter overrides @NonNull parameter

(Edit: Fixed meaning of @NonNull annotation, thanks ci_)

Answer:

Looking at definition of the onCreateDialog method in DialogFragment, you will see:

@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState)

So your code should include the same @NonNull annotation like this:

@Override
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return super.onCreateDialog(savedInstanceState);
}