Skip to content

Nullable is Inconsistent #7617

@gmck

Description

@gmck

Android application type

Android for .NET (net6.0-android, etc.)

Affected platform version

VS 2022 17.5.0 Prev 1

Description

After getting my latest test app built and running in release mode, I then went back and enabled Nullable.
I’m now getting inconsistent results regarding replacing code such as if (Build.VERSION.SdkInt >= BuildVersionCodes.S)) with if (OperatingSystem.IsAndroidVersionAtLeast(31)).

See issue #7586 now closed and @jonathanpeppers reply.

As I progressed through all the .cs files modifying code for nullable, I was replacing code such as the above with OperatingSystem.IsAndroidVersionAtLeast(xx) as I went and the squiggles would go away etc. and I finished up one night with everything corrected except for one file SettingsFragment, which I knew had a number of these to be fixed.

So the next day I started to fix the SettingsFragment which needed other nullable type corrections as well. So I fixed all the other stuff leaving the OperatingSystem.IsAndroidVersionAtLeast(xx) to last.

As I started to fix them, I noticed that a number of them weren’t showing the usual squiggles and they were also building without any warnings.

So now I have a number of them where the new code is commented out, the old code is in play, and I still don’t have any warnings. I’ve tried cleaning and deleting the obj and bin folders, but I can’t make the warnings return using the old code.

Another example that resulted in a crash at runtime. (HelperExplanationDialogFragment)

BottomSheetBehavior? bottomSheetBehavior = BottomSheetBehavior.From((View)view!.Parent!); //OK
BottomSheetBehavior? bottomSheetBehavior = BottomSheetBehavior.From(view!.Parent is View);// Crashed at runtime

The last line was the suggested fix by the IDE.

Error from the log:

attempt to pass an instance of java.lang.Boolean as argument 1 to com.google.android.material.bottomsheet.BottomSheetBehavior com.google.android.material.bottomsheet.BottomSheetBehavior.from(android.view.View)

A final example is probably more of a how-to question.

How to correct the following?

Toast.MakeText(Activity, Resources.GetString(Resource.String.toast_message), ToastLength.Long).Show();

You can do the following, but would you want to?

Toast? toast = Toast.MakeText(Activity!, Resources.GetString(Resource.String.toast_message), ToastLength.Long);
 toast!.Show();

Test app https://github.com/gmck/NavigationGraph7Net7

Steps to Reproduce

See above.

Did you find any workaround?

No

Relevant log output

See above

Metadata

Metadata

Assignees

Labels

Area: App+Library BuildIssues when building Library projects or Application projects.need-attentionA xamarin-android contributor needs to review

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions