networkInfo.IsConnected不会在Xamarin android片段中工作

时间:2018-04-12 15:55:29

标签: c# android xamarin xamarin.android

ConnectivityManager connMgr = (ConnectivityManager)this.Activity.GetSystemService(Android.Content.Context.ConnectivityService);
            NetworkInfo networkInfo = connMgr.ActiveNetworkInfo;
            bool isConnected = networkInfo.IsConnected;

此代码位于我的片段中的onCreateView方法中,当我运行应用程序时,我收到错误:

Unhandled Exception:  
System.NullReferenceException: Object reference not set to an instance of an object. occurred

如果有人向我解释“没有设置为对象的实例意味着什么”,那将是很好的,因为我不太明白。对于xamarin和c#来说还是有点新鲜。

继承了fragament1.cs文件:

using Android.OS;
using Android.Support.V4.App;
using Android.Views;
using Android.Webkit;

namespace TabsApp.Fragments
{
    public class Fragment1 : Fragment
    {
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here
        }

        public static Fragment1 NewInstance()
        {
            var frag1 = new Fragment1 { Arguments = new Bundle() };
            return frag1;
        }

        WebView webView;


        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var ignored = base.OnCreateView(inflater, container, savedInstanceState);

            View v = inflater.Inflate(Resource.Layout.fragment1, container, false);

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

            webView.LoadUrl("https://google.com");

            webView.Settings.JavaScriptEnabled = true;
            webView.SetWebViewClient(new WebViewClient());

            return v;
        }

        public void OnBackPressed()
        {
            webView.GoBack();
        }
    }
}

并且主要是MainActivity.cs文件:

using Android.App;
using Android.OS;
using TabsApp.Fragments;
using Android.Content.PM;

using Android.Support.Design.Widget;
using Android.Support.V7.App;

namespace TabsApp
{
    [Activity(Label = "@string/app_name", MainLauncher = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTop, Icon = "@drawable/icon", 
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)]
    public class MainActivity : AppCompatActivity
    {

        BottomNavigationView bottomNavigation;
        protected override void OnCreate(Bundle bundle)
        {

            base.OnCreate(bundle);
            SetContentView(Resource.Layout.main);
            var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            if (toolbar != null)
            {
                SetSupportActionBar(toolbar);
                SupportActionBar.SetDisplayHomeAsUpEnabled(false);
                SupportActionBar.SetHomeButtonEnabled(false);

            }

            bottomNavigation = FindViewById<BottomNavigationView>(Resource.Id.bottom_navigation);


            bottomNavigation.NavigationItemSelected += BottomNavigation_NavigationItemSelected;

            LoadFragment(Resource.Id.menu_home);
        }

        private void BottomNavigation_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
        {
            LoadFragment(e.Item.ItemId);
        }

        void LoadFragment(int id)
        {

            Android.Support.V4.App.Fragment fragment = null;
            switch (id)
            {
                case Resource.Id.menu_home:
                    fragment = Fragment1.NewInstance();
                    break;
                case Resource.Id.menu_audio:
                    fragment = Fragment2.NewInstance();
                    break;
                case Resource.Id.menu_video:
                    fragment = Fragment3.NewInstance();
                    break;
            }
            if (fragment == null)
                return;

            SupportFragmentManager.BeginTransaction()
               .Replace(Resource.Id.content_frame, fragment)
               .Commit();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这很简单,这对我来说就像你在这里没有你的活动背景一样,因此你无法得到connMgr

你需要做的是当你从你的活动中调用你的片段时,将当前活动传递给你的碎片调用〜片段〜这样的东西:

Fragment1 newInstance(this); 

在构造函数中使用类似的东西在片段中接收它:

public static AppCompatActivity _activity 
public static Fragment1 newInstance(AppCompatActivity activity )
{  
   _activity =activity ;

//Lines of code }

在onCreateView中使用它:

ConnectivityManager connMgr = (ConnectivityManager)_activity.GetSystemService(Android.Content.Context.ConnectivityService);
NetworkInfo networkInfo = connMgr.ActiveNetworkInfo;
        bool isConnected = networkInfo.IsConnected;

如果它不起作用lemme知道!古德勒克