Xamarin新项目遇未处理异常:无法从程序集加载MainActivity求助
Hey Andrew, sorry to hear you're stuck with this frustrating TypeLoadException in your new Xamarin Android project—let's walk through some common fixes that usually resolve this, especially since your old projects work perfectly fine.
System.TypeLoadException for MainActivity 1. Clean & Rebuild the Entire Solution
Start with the most straightforward fix, which often clears up weird assembly mismatches:
- Right-click your solution in Visual Studio and select
Clean Solution - Once that finishes, right-click again and choose
Rebuild Solution - Leftover build artifacts from even default project setups can sometimes cause this kind of version conflict.
2. Fix Duplicate Assembly Version Conflicts
The error message shows duplicate version info (Version=1.0.0.0 and Version=0.0.0.0), which is a key clue:
- Go to your Android project's References and check for any duplicated references or ones with mismatched versions
- Head to your project's
Properties > Applicationtab: confirm theAssembly nameandDefault namespaceboth matchLønCalc(this is the default for new projects, but sometimes it gets messed up)
3. Verify MainActivity Has Correct Attributes & Namespace
Even auto-generated code can sometimes get corrupted (rare, but possible):
- Open
MainActivity.csand double-check the top of the class has these required attributes:[Activity(Label = "@string/app_name", MainLauncher = true)] public class MainActivity : AppCompatActivity { // Auto-generated code here } - Make sure the namespace at the top is
namespace LønCalc;(it should match your assembly name exactly)
4. Check the Android Manifest for Correct Activity Entry
If the manifest has a mismatched entry for MainActivity, the system can't find it:
- If you're using the default auto-generated manifest (recommended), ensure you haven't manually added a conflicting
MainActivityentry elsewhere - If you're using a manual
AndroidManifest.xml, confirm the activity block looks like this:<activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
5. Update Visual Studio & Xamarin Components
A recent update might have broken the new project template:
- Go to
Tools > Get Tools and Featuresin Visual Studio, check for updates to the Mobile development with .NET workload - Update all NuGet packages in your new project (even default ones) to their latest stable versions
6. Manually Delete Bin/Obj Folders
Sometimes the "Clean Solution" command misses cached files:
- Close Visual Studio completely
- Navigate to your project's folder, delete the
binandobjfolders for both the Android project and the solution root - Reopen Visual Studio and rebuild the solution from scratch
If none of these steps work, take a look at the Output window in Visual Studio during the build—there might be hidden warnings that point to the real issue. Let me know what you find!
内容的提问来源于stack exchange,提问作者Andrew Z

