C# 我的xamarin android应用程序在重新启动时崩溃。使用带有导航抽屉布局的片段

C# 我的xamarin android应用程序在重新启动时崩溃。使用带有导航抽屉布局的片段,c#,android,android-fragments,xamarin.android,C#,Android,Android Fragments,Xamarin.android,我对Xamarin和Android编程还不熟悉,我正在为一个例外而挣扎 我的应用程序使用导航抽屉布局和片段 它有一个存储在android设备上的数据文件,其中包含要加载的配置文件。它们内置在URL中,用于片段上的webview控件 当应用程序第一次运行时,它会请求访问外部存储的权限(我正在为此使用plugin.permissions包)。应用程序等待权限,然后打开文件并将配置文件选为默认配置文件。然后它运行一个过程(将概要文件作为参数),这将在主布局中创建片段。将有一个从fragment类调用的

我对Xamarin和Android编程还不熟悉,我正在为一个例外而挣扎

我的应用程序使用导航抽屉布局和片段

它有一个存储在android设备上的数据文件,其中包含要加载的配置文件。它们内置在URL中,用于片段上的webview控件

当应用程序第一次运行时,它会请求访问外部存储的权限(我正在为此使用plugin.permissions包)。应用程序等待权限,然后打开文件并将配置文件选为默认配置文件。然后它运行一个过程(将概要文件作为参数),这将在主布局中创建片段。将有一个从fragment类调用的方法,这就是我创建对象而不是在SupportFragmentManager.BeginTransaction行中创建对象的原因

检查配置文件参数是否为null。如果不是,那么我调用fragment方法_permissionsGranted已传递,因此如果应用程序没有访问文件的权限,则可以在webview上显示错误消息

当应用程序首次安装并等待授予权限时,这一切都可以正常工作。重新启动后,调出最近事件列表并返回应用程序都会导致应用程序崩溃。

我得到这个错误:

时间设备名称类型PID标签信息01-02 10:54:14.003三星 SM-G930F Info 15603单机器人系统。NullReferenceException:对象 引用未设置为对象的实例。在 WebRDT2.Fragment_Classes.WebViewFragment.OpenURL (WebRDT2.Classes.environmentprofile _环境,System.Boolean _C:\Users\eversonm\source\repos\WebRDT2\WebRDT2\Fragment中的permissionsGranted)[0x00014] Classes\WebViewFragment.cs:85位于WebRDT2.MainActivity.LoadProfile (WebRDT2.Classes.environmentprofile_ep)[0x00081]位于 C:\Users\eversonm\source\repos\WebRDT2\WebRDT2\MainActivity.cs:236
在WebRDT2.MainActivity.OnCreate(Android.OS.Bundle)上 中的savedInstanceState)[0x002fd] C:\Users\eversonm\source\repos\WebRDT2\WebRDT2\MainActivity.cs:121
在 System.Runtime.CompilerServices.AsyncMethodBuilderCore+c.b\u 7\u 0 中的(System.Object状态)[0x00000] :0 at Android.App.SyncContext+c_uuuu显示类2_0.b_uu0()[0x00000] in:0 at 中的Java.Lang.Thread+RunnableImplementor.Run()[0x00008] :0 at Java.Lang.IRunnableInvoker.n_运行(System.IntPtr jnienv,System.IntPtr 本机\uuu此[0x00009]位于:0处 (包装器动态方法) Android.Runtime.DynamicMethodNameCounter.22(intptr,intptr)

这是指webview对象wv。我知道这一点,因为我从这个方法中取出了使用wv的代码,应用程序不会崩溃。它只调用该方法,只清除cookies

我已通读了这本书

但我不明白问题是什么。再说一次,我是新手

MainActivity.cs(工作正在进行中,因此不需要提示,只要我有办法重新编码webview片段)

名称空间WebRDT2
{
[活动(Label=“@string/app_name”,Theme=“@style/AppTheme”,MainLauncher=true)]
公共类MainActivity:AppCompativeActivity,NavigationView.IOnNavigationItemSelectedListener
{
名单;
Plugin.Permissions.Abstractions.PermissionStatus PermissionStatus;
Bundle main savedinstancestate;
创建时受保护的异步重写void(Bundle savedInstanceState)
{
Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(此为savedInstanceState);
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(这个,savedInstanceState);
//设置权限的当前活动
Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity=此;
//如果需要,请请求权限
等待CrossPermissions.Current.RequestPermissionsAsync(Plugin.Permissions.Abstractions.Permission.Storage);
//检查是否授予了权限
permissionStatus=等待CrossPermissions.Current.CheckPermissionStatusAsync(Plugin.Permissions.Abstractions.Permission.Storage);
main savedInstanceState=savedInstanceState;
//若我们收到一个配置文件列表,那个么将else报告问题加载到用户
if(envList!=null)
{
EnvironmentProfile ep=null;
如果(mainSavedInstanceState==null)
{
//然后第一次加载应用程序
负荷剖面图(ep);
}
}
}
专用void加载配置文件(环境配置文件)
{
Fragment_Classes.WebViewFragment wvf=新的Fragment_Classes.WebViewFragment();
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.fragment_容器,wvf)
.Commit();
SupportFragmentManager.ExecutePendingTransactions();
如果(_ep!=null)
{
OpenURL(_ep,true);
Toast.MakeText(Application.Context,“\u ep不为null”,ToastLength.Long.Show();
}
}
public override void OnRequestPermissionsResult(int-requestCode,string[]permissions,[GeneratedEnum]Android.Content.PM.Permission[]grantResults)
{
PermissionsImplementation.Current.OnRequestPermissionsResult(请求代码、权限、grantResults);
//base.OnRequestPermissionsResult(请求代码、权限、GrantResult);
}
}
}
WebViewFragment.cs

namespace WebRDT2.Fragment_Classes
{
    class WebViewFragment : Fragment
    {
        private View view;

        WebView wv;


        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            //LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.Inflate(Resource.Layout.webview, container, false);

            return view;
        }

        public override void OnViewCreated(View view, Bundle savedInstanceState)
        {
            base.OnViewCreated(view, savedInstanceState);

            wv = view.FindViewById<WebView>(Resource.Id.webView);
        }


        public override void OnStart()
        {
            base.OnStart();

            wv = view.FindViewById<WebView>(Resource.Id.webView);
        }


        public override void OnResume()
        {
            base.OnResume();

            wv = view.FindViewById<WebView>(Resource.Id.webView);
        }


        public void OpenURL(EnviromentProfile _environment, bool _permissionsGranted)
        {
            //WebView wv = view.FindViewById<WebView>(Resource.Id.webView);

            var cookieManager = CookieManager.Instance;
            cookieManager.RemoveAllCookie(); // Clear cache

            string myURL = ""; // URL to load

            wv.SetWebViewClient(new XPOWebViewClient());
            wv.Settings.JavaScriptEnabled = true;
            wv.Settings.BuiltInZoomControls = true;
            wv.Settings.SetSupportZoom(true);
            wv.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;
            wv.ScrollbarFadingEnabled = false;

            wv.LoadDataWithBaseURL(null, "<html><body>---TESTING---</body></html>",
                    "text/html", "utf-8", null);
        }
    }
}
名称空间WebRDT2.Fragment\u类
{
类WebViewFragment:Fragment
{
私人视野;
网络视图;
创建视图上的公共覆盖视图(布局、充气机、视图组容器、捆绑包保存状态)
{
//扁平炎
namespace WebRDT2.Fragment_Classes
{
    class WebViewFragment : Fragment
    {
        private View view;

        WebView wv;


        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            //LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.Inflate(Resource.Layout.webview, container, false);

            return view;
        }

        public override void OnViewCreated(View view, Bundle savedInstanceState)
        {
            base.OnViewCreated(view, savedInstanceState);

            wv = view.FindViewById<WebView>(Resource.Id.webView);
        }


        public override void OnStart()
        {
            base.OnStart();

            wv = view.FindViewById<WebView>(Resource.Id.webView);
        }


        public override void OnResume()
        {
            base.OnResume();

            wv = view.FindViewById<WebView>(Resource.Id.webView);
        }


        public void OpenURL(EnviromentProfile _environment, bool _permissionsGranted)
        {
            //WebView wv = view.FindViewById<WebView>(Resource.Id.webView);

            var cookieManager = CookieManager.Instance;
            cookieManager.RemoveAllCookie(); // Clear cache

            string myURL = ""; // URL to load

            wv.SetWebViewClient(new XPOWebViewClient());
            wv.Settings.JavaScriptEnabled = true;
            wv.Settings.BuiltInZoomControls = true;
            wv.Settings.SetSupportZoom(true);
            wv.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;
            wv.ScrollbarFadingEnabled = false;

            wv.LoadDataWithBaseURL(null, "<html><body>---TESTING---</body></html>",
                    "text/html", "utf-8", null);
        }
    }
}
private void LoadProfile()
{
    Fragment_Classes.WebViewFragment wvf = new Fragment_Classes.WebViewFragment();

    SupportFragmentManager.BeginTransaction()
        .Replace(Resource.Id.fragment_container, wvf)
        .Commit();

    SupportFragmentManager.ExecutePendingTransactions();
}
public override void OnResume()
{
    base.OnResume();

    wv = view.FindViewById<WebView>(Resource.Id.webView);

    ((MainActivity)this.Activity).OpenURL(this);
}
public void OpenURL(Fragment_Classes.WebViewFragment wvf)
{
    wvf.OpenURL(ep, true);
}