2010-11-05 83 views
9

錯誤日誌:「TypeInitializationException是未處理」

System.TypeInitializationException was unhandled 
    Message="The type initializer for 'MaxDavidMP4.Program' threw an exception." 
    Source="MaxDavidMP4" 
    TypeName="MaxDavidMP4.Program" 
    StackTrace: 
     at MaxDavidMP4.Program.Main() 
     at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: System.IO.FileNotFoundException 
     Message="Could not load file or assembly 'Microsoft.Xna.Framework, Version=3.1.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d' or one of its dependencies. The system cannot find the file specified." 
     Source="MaxDavidMP4" 
     FileName="Microsoft.Xna.Framework, Version=3.1.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d" 
     FusionLog="=== Pre-bind state information ===\r\nLOG: User = Max-PC\\Max\r\nLOG: DisplayName = Microsoft.Xna.Framework, Version=3.1.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d\n (Fully-specified)\r\nLOG: Appbase = file:///C:/Users/Max/Desktop/maximas save/School/University/CSS 450/MaxDavidMP4/MaxDavidMP4/bin/Debug/\r\nLOG: Initial PrivatePath = NULL\r\nCalling assembly : UWBGL_XNA_Lib10, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.\r\n===\r\nLOG: This bind starts in default load context.\r\nLOG: No application configuration file found.\r\nLOG: Using machine configuration file from C:\\Windows\\Microsoft.NET\\Framework64\\v2.0.50727\\config\\machine.config.\r\nLOG: Post-policy reference: Microsoft.Xna.Framework, Version=3.1.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d\r\nLOG: The same bind was seen before, and was failed with hr = 0x80070002.\r\n" 
     StackTrace: 
      at MaxDavidMP4.Model..ctor() 
      at MaxDavidMP4.Program..cctor() in C:\Users\Max\Desktop\maximas save\School\University\CSS 450\MaxDavidMP4\MaxDavidMP4\Program.cs:line 14 
     InnerException: 

的Program.cs代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Windows.Forms; 

namespace MaxDavidMP4 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     /// 
     static Model model = new Model(); 

     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form1()); 
     } 

     public static Model getModel() 
     { 
      return model; 
     } 
    } 
} 

看來問題是與Microsoft.Xna.Framework 3.1.0包含,但我確定路徑是正確的,並且我的所有其他項目都可以在此設置下正常工作。這是在VS2008,C#中,順便說一句。我記得上次我遇到這個問題時,我不得不在VS2008中將最重要的中心下拉列表中的一個設置爲「x86」,並修復了它,但現在它沒有任何區別。

+1

單擊調試 - >異常並檢查所有投擲複選框。這將幫助您確定問題。 – 2014-06-18 14:46:33

回答

10

這很可能是您上次遇到同樣的問題。我想不出你會得到這種例外的另一個原因。很可能您沒有正確實施修復程序。

請注意,這是內部例外,在這裏很重要。如果您在程序的主體中創建了模型,而不是在靜態初始化期間創建模型,則外部異常將消失。

所以你的異常基本上是:

未能加載程序集 'Microsoft.Xna.Framework'。該系統找不到指定的文件。

Shawn Hargreaves writes該異常真正含義是:

未能加載32位彙編 'Microsoft.Xna.Framework' 變爲64位的過程。你的遊戲項目被設置爲'任何CPU'平臺,當它指定'x86'時。

修復的方法是:

在你的Visual Studio工具欄上,應該有一個組合框,說 '任何CPU'。

如果您使用的是C#Express,則此工具欄條目可能會變灰。要啓用它,請轉至'工具/選項',選中'顯示所有設置',選擇'項目和解決方案'選項卡,並選中'顯示高級構建配置'複選框。

拉下'Any CPU'工具欄組合,然後選擇'配置管理器'。打開「主動解決方案平臺」組合,選擇'<New...>',並創建'x86'配置。

6

我在我的程序Main()的第一行出現了相同的錯誤,它幾天來一直運行得很好。它與裝配組件無關。問題是我不小心將此電話粘貼到同一工具類的聲明部分中的靜態工具類'方法。例如:

public class Utility 
{ 
    public static int i = Utility.SomeStaticMethod(); 

    public static int SomeStaticMethod() 
    { 
     return 1; 
    } 
} 

我不完全理解發生了什麼上述內部膽量,但我知道這是問題,因爲只要我感動的是呼叫到一個init()方法中,TypeInitializationException消失。我的猜測是,雖然上面的內容在編譯器POV中是合法的,但是在運行時遇到了尚未定義的方法的使用。由於這是作爲一個完全靜態的類使用的,所以在沒有正常的異常流程的場景之後,類不會像其他所有內容那樣「初始化」,而是以某種方式初始化。

想法?

+0

我得到與CodedMonkey相同的異常。你的解決方案有效 – user35443 2012-09-01 12:18:08

+0

說得很簡單:錯誤在代碼中。調試靜態初始化的初始化。我剛剛發現我在初始化時做錯了什麼。作爲Jon。 Black建議,這可能有助於在靜態構造函數中組織此初始化代碼,這是調試的第一步。 – Oren 2014-10-03 13:31:35