2013-06-26 87 views
0

在mvvmCross中使用片段時,存在以下問題。我借用了https://github.com/slodge/MvvmCross-Tutorials/tree/master/Fragments的代碼。該Framepresenter類,這是在https://github.com/slodge/MvvmCross-Tutorials/blob/master/Fragments/FragmentSample.UI.Droid/Setup.cs解析請求展示在展示方法mvvmCross中片段的生命週期

 
public class CustomPresenter 
     : MvxAndroidViewPresenter 
     , ICustomPresenter 
    { 
     private Dictionary _dictionary = new Dictionary(); 

     public override void Show(MvxViewModelRequest request) 
     { 
      IFragmentHost host; 
      if (_dictionary.TryGetValue(request.ViewModelType, out host)) 
      { 
       if (host.Show(request)) 
       { 
        return; 
       } 
      } 

      base.Show(request); 
     } 
... 

在第一次運行一切運作良好的ViewModels。如果我用android的「Back」按鈕關閉應用程序並再次打開它,我將在基類(MvxAndroidViewPresenter)的Show方法中得到空指針異常。這是因爲頂級活動不再存在。 MvxAndroidViewPresenter中的屬性Activity返回null。如何在重新啓動應用程序時重新創建頂級活動?

我得到下面的異常

 
06-26 16:06:28.102 I/MonoDroid(6791): UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object 
06-26 16:06:28.102 I/MonoDroid(6791): at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewPresenter.Show (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest) [0x0000f] in d:\Projekte\MvvmCross\mvvmcross-20130621\Cirrious\Cirrious.MvvmCross.Droid\Views\MvxAndroidViewPresenter.cs:29 
06-26 16:06:28.102 I/MonoDroid(6791): at ManCockpit.UI.Droid.CockpitFragmentPresenter.Show (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest) [0x00040] in d:\Projekte\ManCockpitV2\ManCockpit.UI.Droid\CockpitFragmentPresenter.cs:41 
06-26 16:06:28.102 I/MonoDroid(6791): at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewDispatcher/c__DisplayClass1.b__0() [0x00000] in d:\Projekte\MvvmCross\mvvmcross-20130621\Cirrious\Cirrious.MvvmCross.Droid\Views\MvxAndroidViewDispatcher.cs:27 
06-26 16:06:28.102 I/MonoDroid(6791): at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewDispatcher.ShowViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest) [0x00015] in d:\Projekte\MvvmCross\mvvmcross-20130621\Cirrious\Cirrious.MvvmCross.Droid\Views\MvxAndroidViewDispatcher.cs:27 
06-26 16:06:28.102 I/MonoDroid(6791): at Cirrious.MvvmCross.ViewModels.MvxNavigatingObject.ShowViewModelImpl (System.Type,Cirrious.MvvmCross.ViewModels.IMvxBundle,Cirrious.MvvmCross.ViewModels.IMvxBundle,Cirrious.MvvmCross.ViewModels.MvxRequestedBy) 
06-26 16:06:28.102 I/MonoDroid(6791): at (wrapper dynamic-method) objec06-26 16:06:28.102 I/MonoDroid(6791): at Cirrious.MvvmCross.ViewModels.MvxNavigatingObject.ShowViewModel (System.Type,Cirrious.MvvmCross.ViewModels.IMvxBundle,Cirrious.MvvmCross.ViewModels.IMvxBundle,Cirrious.MvvmCross.ViewModels.MvxRequestedBy) 
06-26 16:06:28.102 I/MonoDroid(6791): at Cirrious.MvvmCross.ViewModels.MvxNavigatingObject.ShowViewModel (Cirrious.MvvmCross.ViewModels.IMvxBundle,Cirrious.MvvmCross.ViewModels.MvxBundle,Cirrious.MvvmCross.ViewModels.MvxRequestedBy) 
06-26 16:06:28.102 I/MonoDroid(6791): at Cirrious.MvvmCross.ViewModels.MvxAppStart`1.Start (object) 
06-26 16:06:28.102 I/MonoDroid(6791): at Cirrious.MvvmCross.Droid.Views.MvxSplashScreenActivity.TriggerFirstNavigate() [0x00007] in d:\Projekte\MvvmCross\mvvmcross-20130621\Cirrious\Cirrious.MvvmCross.Droid\Views\MvxSplashScreenActivity.cs:82 
06-26 16:06:28.102 I/MonoDroid(6791): at Cirrious.MvvmCross.Droid.Views.MvxSplashScreenActivity.InitializationComplete() [0x00001] in d:\Projekte\MvvmCross\mvvmcross-20130621\Cirrious\Cirrious.MvvmCross.Droid\Views\MvxSplashScreenActivity.cs:76 
06-26 16:06:28.102 I/MonoDroid(6791): at Cirrious.MvvmCross.Droid.Platform.MvxAndroidSetupSingleton.InitialiseFromSplashScreen (Cirrious.MvvmCross.Droid.Views.IMvxAndroidSplashScreenActivity) [0x00036] in d:\Projekte\MvvmCross\mvvmcross-20130621\Cirrious\Cirrious.MvvmCross.Droid\Platform\MvxAndroidSetupSingleton.cs:76 
06-26 16:06:28.102 I/MonoDroid(6791): at Cirrious.MvvmCross.Droid.Views.MvxSplashScreenActivity.OnCreate (Android.OS.Bundle) [0x00014] in d:\Projekte\MvvmCross\mvvmcross-20130621\Cirrious\Cirrious.MvvmCross.Droid\Views\MvxSplashScreenActivity.cs:47 
06-26 16:06:28.102 I/MonoDroid(6791): at ManCockpit.UI.Droid.SplashScreenActivity.OnCreate (Android.OS.Bundle) [0x0002e] in d:\Projekte\ManCockpitV2\ManCockpit.UI.Droid\SplashScreenActivity.cs:105 
06-26 16:06:28.102 I/MonoDroid(6791): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00010] in /Users/builder/data/lanes/monodroid-lion-bs1/0cc7ae3b/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.App.Activity.cs:1859 
06-26 16:06:28.102 I/MonoDroid(6791): at (wrapper dynamic-method) object.08473369-0d89-4344-a57c-40e97793bab8 (intptr,intptr,intptr) 
+0

你可以發佈你所看到的例外 - 它可能只是這個問題 - https://github.com/slodge/MvvmCross/issues/316 - 已解決但尚未發佈。 – Stuart

+0

我添加了異常 – user2524296

回答

0

這看起來這是無關的片段 - 但不是一個已知的錯誤 - https://github.com/slodge/MvvmCross/issues/316

對此問題進行修復尚未發佈的NuGet。

要解決該問題,您可以自行構建修復程序。或者你也可以通過添加覆蓋您的SplashScreen類行爲:

 private bool _isResumed; 

    protected override void OnResume() 
    { 
     _isResumed = true; 
     base.OnResume(); 
    } 

    protected override void OnPause() 
    {   
     _isResumed = false; 
     base.OnPause(); 
    } 

    public override void InitializationComplete() 
    { 
     if (!_isResumed) 
      return; 

     TriggerFirstNavigate(); 
    } 
+0

如果我將上面的代碼添加到碎片示例的splashscreen中,啓動畫面會在恢復應用程序時永久顯示。 – user2524296

+0

只是一個訂購問題(試圖修補從https://github.com/slodge/MvvmCross/commit/8420391388656e680fad5b54208d57aa78a72db9修復) - 嘗試更新的OnResume在答案 – Stuart

+0

這就是它!非常感謝你! – user2524296