9

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.

user3339562
  • 1,325
  • 4
  • 18
  • 34
  • 1
    possible duplicate of [Meaning of Android Studio error: Not annotated parameter overrides @NotNull parameter](http://stackoverflow.com/questions/24728627/meaning-of-android-studio-error-not-annotated-parameter-overrides-notnull-para) – JHH Mar 23 '15 at 15:53

2 Answers2

17

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);
}
Sean Lao
  • 171
  • 4
6

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_)

Community
  • 1
  • 1
JHH
  • 8,567
  • 8
  • 47
  • 91
  • Thank you. I've tried to import `android.support.annotation.NoNNull` but Android Studio can't find annotation. That solution doesn't work for me. – user3339562 Mar 23 '15 at 15:56
  • 2
    @JHH you didn't read the question right, the principle is the same, but the details are different. In this case the method is annotated with @NonNull, which means you cannot return `null` from it. The `savedInstanceState` parameter can very well be null. This case is even specifically mentioned in the docs. – ci_ Jun 16 '15 at 13:51
  • Thanks @ci_. Apparently I was too quick. – JHH Jun 20 '15 at 16:49