0

When update project Kotlin Android targetSdkVersion from 29 to 30, The following code line has an error.

webview.loadUrl(null) // error here: Null can not be a value of a non-null type String

I checked the function declaration, it does not change anything from API 29 to 30.

public void loadUrl(String url) {
}

What is the cause of the error in API 30 ? and why it not occurred in API 29?

I'm using Android Studio 4.0.1

Any help would be greatly appreciated.

larva
  • 4,687
  • 1
  • 24
  • 44
  • 1
    The Javadocs for this method say that the passed `url` must never be null, so you shouldn't be passing null anyway. https://developer.android.com/reference/android/webkit/WebView#loadUrl(java.lang.String) It's possible they added a nullability annotation in between API 29 and 30. The code you posted is from the MockView version of WebView. I don't know why that's what you get when you look at the source in Android Studio. – Tenfour04 Oct 22 '20 at 04:07
  • 1
    You can see the `@NonNull` annotation here: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/webkit/WebView.java#731 – Tenfour04 Oct 22 '20 at 04:12
  • @Tenfour04 Thanks fo comment. When download SDK with AndroidStudio, I can't see any NonNull declaration. And yes, there is a change in nullability annotation in Android 11: https://android-developers.googleblog.com/2020/03/handling-nullability-in-android-11-and.html – larva Oct 22 '20 at 07:33
  • Generally Lint will tell you what the issue is in those cases. You probably cannot see any NonNull inside Android Studio, because it opens the SDK 29 files and not the 30 ones. This will happen if you didn't download the 30 locally or haven't cleared the cache since you upgraded from 29 to 30. – Viktor Stojanov Oct 24 '20 at 15:29

1 Answers1

0

If you are writing in Kotlin, when upgrading from the Android 10 to the Android 11 SDK, you may notice that there are some new compiler warnings and that previous warnings may have been upgraded to errors. This is intended and a feature of the Kotlin compiler — these warnings tell you that you may be writing code that crashes your app at runtime (a risk you would miss entirely if you weren’t writing in Kotlin). As you encounter these warnings and errors, you can handle them by adding null checks to your code.

see https://android-developers.googleblog.com/2020/03/handling-nullability-in-android-11-and.html

zwh
  • 172
  • 2
  • 12