You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

Troubleshooting the 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 > Application tab: confirm the Assembly name and Default namespace both match Lø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.cs and 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 MainActivity entry 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 Features in 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 bin and obj folders 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:09:07