XamarinCommunityToolkit icon indicating copy to clipboard operation
XamarinCommunityToolkit copied to clipboard

[Bug] [MauiCompat] Xamarin.CommunityToolkit.MauiCompat CameraView produces Android No view found for fragment CameraFragment error

Open julianadormon opened this issue 3 years ago • 7 comments

Thanks in advance!! I'm quite new to XAML so be kind :)

Description

My requirement is to add a CameraView to a .net Maui app which runs on Android and iOS. I understand that there is no CameraView in the Maui.Community.Toolkit but I was under the impression, which may be wrong, that I could run the Xamarin.Community.Tookit.MauiCompat packages which I'm trying to do in the code below.

Stack Trace

--- End of managed Java.Lang.IllegalArgumentException stack trace --- java.lang.IllegalArgumentException: No view found for id 0x1 (unknown) for fragment CameraFragment{4e42a40} (ddc40138-4928-4ebc-bfd4-b5fb0b1cacdc id=0x1 tag=camera) at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:513) at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261) at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1840) at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1758) at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1701) at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2849) at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2784) at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:262) at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:478) at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1455) at android.app.Activity.performStart(Activity.java:8076) at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3660) at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221) at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201) at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7839) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

Link to Reproduction Sample

I posted my code to stack-overflow but if you need a repo, please let me know: https://stackoverflow.com/questions/73889560/xamarin-communitytoolkit-mauicompat-cameraview-produces-android-no-view-found-fo

Steps to Reproduce

  1. Create a .net Maui Blazor app
  2. Install and use Xamarin.CommunityToolkit.MauiCompat in MauiProgram.cs
  3. Configure handlers
  4. Add CameraView to XAML and appropriate Event Callbacks as per this demo https://github.com/xamarin/XamarinCommunityToolkit/blob/main/samples/XCT.Sample/Pages/Views/CameraViewPage.xaml https://github.com/xamarin/XamarinCommunityToolkit/blob/main/samples/XCT.Sample/Pages/Views/CameraViewPage.xaml.cs
  5. Run degug to Android Emulator Pixel 5 (API 31)

Expected Behavior

Expect program to build and run

Actual Behavior

Program errors with Java error

Basic Information

  • Version with issue:
  • Last known good version: Xamarin.CommunityToolkit.MauiCompat 2.0.2 1013
  • IDE: Visual Studio Community 2022 for Mac Preview Version 17.4 Preview (17.4 build 1583)
  • Platform Target Frameworks: Android, iOS

Visual Studio Community 2022 for Mac Preview Version 17.4 Preview (17.4 build 1583) Installation UUID: 0b36004a-e5d9-4978-ae2a-2d2f56351cff

Runtime .NET 6.0.5 (64-bit) Architecture: Arm64

Roslyn (Language Service) 4.4.0-2.22419.1+ae5663806be933515723b2d5a85faf5d378f460d

NuGet Version: 6.3.0.128

.NET SDK (Arm64) SDK: /usr/local/share/dotnet/sdk/7.0.100-rc.1.22431.12/Sdks SDK Versions: 7.0.100-rc.1.22431.12 6.0.401 6.0.400 6.0.400-preview.22330.6 MSBuild SDKs: /Applications/Visual Studio (Preview).app/Contents/MonoBundle/MSBuild/Current/bin/Sdks

.NET SDK (x64) SDK Versions: 5.0.408 3.1.423 3.1.422 3.1.421

.NET Runtime (Arm64) Runtime: /usr/local/share/dotnet/dotnet Runtime Versions: 7.0.0-rc.1.22426.10 6.0.9 6.0.8 6.0.6

.NET Runtime (x64) Runtime: /usr/local/share/dotnet/x64/dotnet Runtime Versions: 5.0.17 3.1.29 3.1.28 3.1.27

Xamarin.Profiler Version: 1.8.0.19 Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

Updater Version: 11

Apple Developer Tools Xcode 14.0.1 (21336) Build 14A400

Xamarin.Mac Version: 8.12.0.2 (Visual Studio Community) Hash: 87f98a75e Branch: d17-3 Build date: 2022-07-25 20:18:54-0400

Xamarin.iOS Version: 15.12.0.2 (Visual Studio Community) Hash: 87f98a75e Branch: d17-3 Build date: 2022-07-25 20:18:55-0400

Xamarin Designer Version: 17.4.0.124 Hash: ffbba0c031 Branch: remotes/origin/d17-4 Build date: 2022-09-15 15:36:42 UTC

Xamarin.Android Version: 13.0.99.81 (Visual Studio Community) Commit: xamarin-android/main/7c9c24b Android SDK: /Users/macminim1/Library/Developer/Xamarin/android-sdk-macosx Supported Android versions: 12.0 (API level 31)

SDK Command-line Tools Version: 7.0 SDK Platform Tools Version: 33.0.2 SDK Build Tools Version: 33.0.0

Build Information: Mono: dffa5ab Java.Interop: xamarin/java.interop/main@2c8b0a8f SQLite: xamarin/sqlite/3.39.2@40e8743 Xamarin.Android Tools: xamarin/xamarin-android-tools/main@29f11f2

Microsoft Build of OpenJDK Java SDK: /Library/Java/JavaVirtualMachines/microsoft-11.jdk 11.0.12 Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL

Eclipse Temurin JDK Java SDK: /Library/Java/JavaVirtualMachines/temurin-8.jdk 1.8.0.302 Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL

Android SDK Manager Version: 17.4.0.45 Hash: 8cac7ea Branch: remotes/origin/HEAD~1 Build date: 2022-09-15 15:36:46 UTC

Android Device Manager Version: 0.0.0.1196 Hash: b2faf27 Branch: main~1 Build date: 2022-09-15 15:36:46 UTC

Build Information Release ID: 1704001583 Git revision: 70322329919c72a11d9795a3cb403fe4115a5878 Build date: 2022-09-15 15:34:40+00 Build branch: release-17.4 Build lane: release-17.4

Operating System Mac OS X 12.5.0 Darwin 21.6.0 Darwin Kernel Version 21.6.0 Sat Jun 18 17:05:47 PDT 2022 root:xnu-8020.140.41~1/RELEASE_ARM64_T8101 arm64

Build Information Release ID: 1704001583 Git revision: 70322329919c72a11d9795a3cb403fe4115a5878 Build date: 2022-09-15 15:34:40+00 Build branch: release-17.4 Build lane: release-17.4

Operating System Mac OS X 12.5.0 Darwin 21.6.0 Darwin Kernel Version 21.6.0 Sat Jun 18 17:05:47 PDT 2022 root:xnu-8020.140.41~1/RELEASE_ARM64_T8101 arm64

  • Nuget Packages: <ItemGroup> <PackageReference Include="Blazored.Modal" Version="7.0.0" /> <PackageReference Include="BlazorILogger" Version="1.4.0" /> <PackageReference Include="Microsoft.AspNetCore.Authorization" Version="6.0.9" /> <PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="6.0.9" />

    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.9" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.9">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    
    <PackageReference Include="SQLitePCLRaw.core" Version="2.1.2" />
        <PackageReference Include="SQLitePCLRaw.provider.sqlite3" Version="2.1.2" />
        <PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.2" />
        <PackageReference Include="SQLitePCLRaw.provider.dynamic_cdecl" Version="2.1.2" />
    <PackageReference Include="Microsoft.Extensions.Identity.Core" Version="6.0.9" />
    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.2" />
    <PackageReference Include="Radzen.Blazor" Version="4.1.0" />
    <PackageReference Include="Serilog.Sinks.Seq" Version="5.2.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
    <PackageReference Include="ZXing.Net.Maui" Version="0.1.0-preview.7" />
    <PackageReference Include="BlazorComponentBus" Version="2.1.0" />
    <PackageReference Include="Xamarin.CommunityToolkit.MauiCompat" Version="2.0.2-preview1013" />
    <PackageReference Include="Xamarin.CommunityToolkit.Markup.MauiCompat" Version="2.0.2-preview1013" />
    
  • Affected Devices:

  • MAUI Version:
    Could not find it anywhere but I imagine it recent as this is a new install of Visual Studio Preview for Mac,

Workaround

None

julianadormon avatar Sep 29 '22 10:09 julianadormon

FYI, I also get the same error trying to debug on a physical Android device.

julianadormon avatar Sep 29 '22 10:09 julianadormon

I know we have discussed internally that things like CameraView would not be expected to work well if at all in MauiCompat due to complexities and limitations of the compat layer. I don't know if we ever came to a decision on how to make this publicly known - we did discuss marking as obsolete so warnings would present.

Sadly this isn't going to help solve this but I believe the answer is going to be to not use the CameraView in a MauiCompat based app. At least that is my understanding, hopefully someone else might rubbish my statement

bijington avatar Sep 29 '22 11:09 bijington

Thanks @bijington I was hoping that was not the case. I appreciate your insight.

julianadormon avatar Sep 30 '22 10:09 julianadormon

I have a similar issue with Xamarin.Form 5.0.0.2515 , Xamarin.CommunityToolkit 2.0.5

AndroidRuntime] java.lang.IllegalArgumentException: No view found for id 0x37 (unknown) for fragment CameraFragment{1d6a23a} (af9ca8f7-3e90-410f-9c47-5aa8d6db2ec7 id=0x37 tag=camera)
[AndroidRuntime] 	at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:513)
[AndroidRuntime] 	at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
[AndroidRuntime] 	at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
[AndroidRuntime] 	at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
[AndroidRuntime] 	at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
[AndroidRuntime] 	at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
[AndroidRuntime] 	at android.os.Handler.handleCallback(Handler.java:938)
[AndroidRuntime] 	at android.os.Handler.dispatchMessage(Handler.java:99)
[AndroidRuntime] 	at android.os.Looper.loop(Looper.java:223)
[AndroidRuntime] 	at android.app.ActivityThread.main(ActivityThread.java:7656)
[AndroidRuntime] 	at java.lang.reflect.Method.invoke(Native Method)
[AndroidRuntime] 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
[AndroidRuntime] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Edit: add simple project. CameraViewIssue.zip

workgroupengineering avatar Nov 28 '22 13:11 workgroupengineering

I could be missing something, but it seems like this error prevents Android builds in Release mode:

1>ILLink : error IL8000: Could not find Android Resource '@layout/camerafragment'. Please update @(AndroidResource) to add the missing resource.
1>ILLink : error IL8000: Could not find Android Resource '@layout/camerafragment'. Please update @(AndroidResource) to add the missing resource.
1>ILLink : error IL8000: Could not find Android Resource '@layout/camerafragment'. Please update @(AndroidResource) to add the missing resource.
1>ILLink : error IL8000: Could not find Android Resource '@layout/camerafragment'. Please update @(AndroidResource) to add the missing resource.
1>C:\np\microsoft.net.illink.tasks\8.0.0-preview.7.23375.6\build\Microsoft.NET.ILLink.targets(84,5): error NETSDK1144: Optimizing assemblies for size failed. Optimization can be disabled by setting the PublishTrimmed property to false.
1>Done building project "Project.csproj" -- FAILED.

Will test disabling optimization, but I'm not sure that's a great final solution (shouldn't that be on?). I'm not using a CameraView anywhere in my application.

bradencohen avatar Sep 07 '23 21:09 bradencohen

@bradencohen

The problem here is that this script https://github.com/xamarin/XamarinCommunityToolkit/blob/5a6062f3c3543acda3c36ca4683cd8fc7fe86ba7/MauiCompat.sh#L561 is renaming the code to use all lowercase, but the actual Resource/Layout is still called CameraFragment. This results in a .net/__res_name_case_map.txt being packaged in the .aar file which comes from the NuGet Package. This contains a remap entry Resources/Layout/CameraFragment.axml;layout/camerafragment.xml which tells the Android build system to expect the code to use CameraFragment but the resource file is camerafragment. I'm not sure why the script was run on the code but it does seem to cause issues when upgrading to .net8.0-android.

The fix is to modify your local Xamarin.CommunityToolkit.MauiCompat.aar file to remove the .net/__res_name_case_map.txt file (the aar is just a zip file you can modify). Alternatively someone could fix the casing of the CameraFragment.xml file in this repo to match the lowercase they want in the code. This would mean the remap file would not be needed. Finally I am working on a PR which will work around this problem in https://github.com/xamarin/xamarin-android/pull/8376. But in an ideal world this should not be needed. Having this mismatching code/resource file could cause all sorts of issues.

dellis1972 avatar Sep 27 '23 10:09 dellis1972

I believe the CameraView implementation in the .NET MAUI Community Toolkit is very close now so it would probably be a better move to migrate to using that.

bijington avatar Sep 28 '23 10:09 bijington