Mac版Visual Studio无法预览Android界面问题求助
I've run into similar glitches with the Xamarin Android Designer in VS for Mac before, so here are some practical steps to work through this System.NullReferenceException:
Clean & Rebuild Your Solution
Start with the basics: right-click your project → Clean, then right-click again → Rebuild. Compilation cache quirks often cause the designer to fail parsing code correctly, and a clean rebuild resets this state.Update Visual Studio & Xamarin Components
This error traces back to theCodeModelBridgein the Xamarin Android Designer, which is a known issue in older versions. Open Visual Studio for Mac, go to Visual Studio → Check for Updates, and install all available stable updates for Visual Studio, Xamarin.Android, and Android SDK tools.Verify Your Activity Class Definitions
The error fires when the designer checks if a type is an Activity (IsActivityTypemethod). Double-check your Activity classes:- Make sure they properly inherit from
AppCompatActivityorActivity - Fix any syntax errors, missing using directives, or invalid custom attributes/annotations that might break code analysis
- Confirm all referenced NuGet packages (like Xamarin.Forms if you're using it) are installed correctly
- Make sure they properly inherit from
Reset the Designer Cache
Corrupted designer cache files can cause this crash. Here's how to clear them:- Close Visual Studio for Mac completely
- Open Finder, press
Cmd+Shift+G, and paste this path (replace[VS_Version]with your installed version, e.g.,17.0):~/Library/Caches/VisualStudio/[VS_Version]/Xamarin/AndroidDesigner - Delete all files and folders in this directory
- Reopen Visual Studio and try the preview again
Test with a Fresh Project
Create a new blank Android project and check if the preview works there. If it does, the problem is specific to your current project—gradually migrate code from your broken project to the new one to identify which part is causing the error.
Original Error Stack Trace:
System.NullReferenceException: Object reference not set to an instance of an object at Xamarin.AndroidDesigner.CodeInteractions.CodeModelBridge.b__31_0 (Microsoft.CodeAnalysis.AttributeData a) [0x00000] in /Users/vsts/agent/2.133.3/work/1/s/Xamarin.Designer.Android/Xamarin.AndroidDesigner/Xamarin.AndroidDesigner.CodeInteractions/CodeModelBridge.cs:403 at System.Linq.ImmutableArrayExtensions.Any[T] (System.Collections.Immutable.ImmutableArray`1[T] immutableArray, System.Func`2[T,TResult] predicate) [0x00025] in <36486b016d234fca8cd67892bf29c7b5>:0 at Xamarin.AndroidDesigner.CodeInteractions.CodeModelBridge.IsActivityType (Microsoft.CodeAnalysis.INamedTypeSymbol type) [0x00008] in /Users/vsts/agent/2.133.3/work/1/s/Xamarin.Designer.Android/Xamarin.AndroidDesigner/Xamarin.AndroidDesigner.CodeInteractions/CodeModelBridge.cs:403 at Xamarin.AndroidDesigner.CodeInteractions.CodeModelBridge.GetAndroidEntityForType (Microsoft.CodeAnalysis.INamedTypeSymbol type) [0x00001] in /Users/vsts/agent/2.133.3/work/1/s/Xamarin.Designer.Android/Xamarin.AndroidDesigner/Xamarin.AndroidDesigner.CodeInteractions/CodeModelBridge.cs:120 at Xamarin.AndroidDesigner.CodeInteractions.CodeModelBridge.b__26_2 (Microsoft.CodeAnalysis.INamedTypeSymbol t) [0x00000] in /Users/vsts/agent/2.133.3/work/1/s/Xamarin.Designer.Android/Xamarin.AndroidDesigner/Xamarin.AndroidDesigner.CodeInteractions/CodeModelBridge.cs:311 at System.Linq.Utilities+<>c__DisplayClass1_0`1[TSource].<CombinePredicates>b__0 (TSource x) [0x00000] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/external/corefx/src/System.Linq/src/System/Linq/Utilities.cs:58 at System.Linq.Enumerable+WhereSelectEnumerableIterator`2[TSource,TResult].ToList () [0x0001b] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/external/corefx/src/System.Linq/src/System/Linq/Where.cs:737 at System.Linq.Enumerable.ToList[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x0000e] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/external/corefx/src/System.Linq/src/System/Linq/ToCollection.cs:30 at Xamarin.AndroidDesigner.CodeInteractions.CodeModelBridge+<GetCustomComponentsInProject>d__26.MoveNext () [0x00147] in /Users/vsts/agent/2.133.3/work/1/s/Xamarin.Designer.Android/Xamarin.AndroidDesigner/Xamarin.AndroidDesigner.CodeInteractions/CodeModelBridge.cs:308 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357 at Xamarin.AndroidDesigner.DesignerProject+d__101.MoveNext () [0x00025] in /Users/vsts/agent/2.133.3/work/1/s/Xamarin.Designer.Android/Xamarin.AndroidDesigner/Xamarin.AndroidDesigner/DesignerProject.cs:300 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
内容的提问来源于stack exchange,提问作者AppsThatFly

