title: Cannot access a nonstatic member of outer type... via nested type
body: <p>Why do I get this error?</p>
  
  <blockquote>
    <p>Error CS0038: Cannot access a nonstatic member of outer type
    <code>JsonFeedParserTabs.MainActivity' via nested type
    </code>JsonFeedParserTabs.MainActivity.SampleTabFragment' (CS0038)
    (JsonFeedParserTabs)</p>
  </blockquote>
  
  <p>I'm trying to put a ListView with json data inside a tab.</p>
  
  <p>This is my code:</p>
  
  <pre><code>using System;
  using Android.App;
  using Android.Content;
  using Android.Runtime;
  using Android.Views;
  using Android.Widget;
  using Android.OS;
  using System.Threading.Tasks;
  using System.Net.Http;
  using Newtonsoft.Json;
  using System.Linq;
  
  namespace JsonFeedParserTabs
  {
      [Activity (Label = "Feed Reader", MainLauncher = true, Icon = "@drawable/icon")]
      public class MainActivity : Activity
      {
          ListView listView;
  //      ProgressBar progressBar;
          RootObject result;
  
          string url = "http://javatechig.com/api/get_category_posts/?dev=1&amp;slug=android";
  
          protected override void OnCreate (Bundle bundle)
          {
              base.OnCreate (bundle);
  
              // Set our view from the "main" layout resource.
              SetContentView (Resource.Layout.Main);
  
              this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
  
              AddTab (" Tab 1", new SampleTabFragment ());
  //          AddTab (" Tab 2", new SampleTabFragment2 ());
  
              if (bundle != null)
                  this.ActionBar.SelectTab(this.ActionBar.GetTabAt(bundle.GetInt("tab")));
          }
  
          protected override void OnSaveInstanceState(Bundle outState)
          {
              outState.PutInt("tab", this.ActionBar.SelectedNavigationIndex);
  
              base.OnSaveInstanceState(outState);
          }
  
          void AddTab (string tabText, Fragment view)
          {
              var tab = this.ActionBar.NewTab ();
  
              tab.SetText (tabText);
  
              // Must set event handler before adding tab.
              tab.TabSelected += delegate(object sender, ActionBar.TabEventArgs e) {
                  var fragment = this.FragmentManager.FindFragmentById(Resource.Id.fragmentContainer);
  
                  if (fragment != null)
                      e.FragmentTransaction.Remove(fragment);
  
                  e.FragmentTransaction.Add (Resource.Id.fragmentContainer, view);
              };
              tab.TabUnselected += delegate(object sender, ActionBar.TabEventArgs e) {
                  e.FragmentTransaction.Remove(view);
              };
  
              this.ActionBar.AddTab (tab);
          }
  
          class SampleTabFragment : Fragment
          {
              public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
              {
                  base.OnCreateView (inflater, container, savedInstanceState);
  
                  var view = inflater.Inflate (Resource.Layout.Tab, container, false);
  
                  // Initializing listView.
                  listView = view.FindViewById&lt;ListView&gt; (Resource.Id.listView); // &lt;-- Error!
                  listView.ItemClick += OnListItemClick; // &lt;-- Error!
  
  //              progressBar = view.FindViewById&lt;ProgressBar&gt; (Resource.Id.progressBar);
  //  
  //              // Showing loading progressBar.
  //              progressBar.Visibility = ViewStates.Visible;
  
                  // Download and display data in url.
                  downloadJsonFeedAsync (url); // &lt;-- Error!
  
                  return view;
              }
          }
  
  //      class SampleTabFragment2 : Fragment
  //      {
  //          public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  //          {
  //              base.OnCreateView(inflater, container, savedInstanceState);
  //
  //              var view = inflater.Inflate(Resource.Layout.Tab, container, false);
  //              var sampleTextView = view.FindViewById&lt;TextView&gt;(Resource.Id.sampleTextView);
  //
  //              sampleTextView.Text = "Sample fragment text 2.";
  //
  //              return view;
  //          }
  //      }
  
          public async void downloadJsonFeedAsync(String url)
          {
              var httpClient = new HttpClient();
              Task&lt;string&gt; contentsTask = httpClient.GetStringAsync(url);
  
              // Await! control returns to the caller and the task continues to run on another thread.
              string content = await contentsTask;
              Console.Out.WriteLine("Response Body: \r\n {0}", content);
  
              // Convert string to JSON object.
              result = Newtonsoft.Json.JsonConvert.DeserializeObject&lt;RootObject&gt; (content);
  
              // Update listview.
              RunOnUiThread (() =&gt; {
                  listView.Adapter = new CustomListAdapter(this, result.posts);
  //              progressBar.Visibility = ViewStates.Gone;
              });
          }
  
          void OnListItemClick(object sender, AdapterView.ItemClickEventArgs e)
          {
              Post item = result.posts.ElementAt (e.Position);
  
              // Passing object form one activity to other.
              Intent i = new Intent(Application.Context, typeof(FeedDetailsActivity));
              i.PutExtra("item", JsonConvert.SerializeObject(item));
              StartActivity(i);
          }
      }
  }
  </code></pre>
  
  <p>I'm stuck and need help, any ideas what I have done wrong and what to do? Thank you!</p>
  
  <h3>Update</h3>
  
  <p>Alright it works now but i think there might be a better way to do this.</p>
  
  <pre><code>using System;
  using Android.App;
  using Android.Content;
  using Android.Runtime;
  using Android.Views;
  using Android.Widget;
  using Android.OS;
  using System.Threading.Tasks;
  using System.Net.Http;
  using Newtonsoft.Json;
  using System.Linq;
  
  namespace JsonFeedParserTabs
  {
      [Activity (Label = "Feed Reader", MainLauncher = true, Icon = "@drawable/icon")]
      public class MainActivity : Activity
      {
          ListView listView;
  //      ProgressBar progressBar;
          RootObject result;
  
          string url = "http://javatechig.com/api/get_category_posts/?dev=1&amp;slug=android";
  
          protected override void OnCreate (Bundle bundle)
          {
              base.OnCreate (bundle);
  
              // Set our view from the "main" layout resource.
              SetContentView (Resource.Layout.Main);
  
              this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
  
              AddTab (" Tab 1", new SampleTabFragment (this));
  //          AddTab (" Tab 2", new SampleTabFragment2 ());
  
              if (bundle != null)
                  this.ActionBar.SelectTab(this.ActionBar.GetTabAt(bundle.GetInt("tab")));
          }
  
          protected override void OnSaveInstanceState(Bundle outState)
          {
              outState.PutInt("tab", this.ActionBar.SelectedNavigationIndex);
  
              base.OnSaveInstanceState(outState);
          }
  
          void AddTab (string tabText, Fragment view)
          {
              var tab = this.ActionBar.NewTab ();
  
              tab.SetText (tabText);
  
              // Must set event handler before adding tab.
              tab.TabSelected += delegate(object sender, ActionBar.TabEventArgs e) {
                  var fragment = this.FragmentManager.FindFragmentById(Resource.Id.fragmentContainer);
  
                  if (fragment != null)
                      e.FragmentTransaction.Remove(fragment);
  
                  e.FragmentTransaction.Add (Resource.Id.fragmentContainer, view);
              };
              tab.TabUnselected += delegate(object sender, ActionBar.TabEventArgs e) {
                  e.FragmentTransaction.Remove(view);
              };
  
              this.ActionBar.AddTab (tab);
          }
  
          class SampleTabFragment : Fragment
          {
              private MainActivity context;
              public SampleTabFragment(MainActivity _context) : base()
              {
                  this.context = _context;
              }
  
              public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
              {
                  base.OnCreateView (inflater, container, savedInstanceState);
  
                  var view = inflater.Inflate (Resource.Layout.Tab, container, false);
  
                  // Initializing listView.
                  context.listView = view.FindViewById&lt;ListView&gt; (Resource.Id.listView);
                  context.listView.ItemClick += context.OnListItemClick;
  
  //              progressBar = view.FindViewById&lt;ProgressBar&gt; (Resource.Id.progressBar);
  //  
  //              // Showing loading progressBar.
  //              progressBar.Visibility = ViewStates.Visible;
  
                  // Download and display data in url.
                  context.downloadJsonFeedAsync (context.url);
  
                  return view;
              }
          }
  
  //      class SampleTabFragment2 : Fragment
  //      {
  //          public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  //          {
  //              base.OnCreateView(inflater, container, savedInstanceState);
  //
  //              var view = inflater.Inflate(Resource.Layout.Tab, container, false);
  //              var sampleTextView = view.FindViewById&lt;TextView&gt;(Resource.Id.sampleTextView);
  //
  //              sampleTextView.Text = "Sample fragment text 2.";
  //
  //              return view;
  //          }
  //      }
  
          public async void downloadJsonFeedAsync(String url)
          {
              var httpClient = new HttpClient();
              Task&lt;string&gt; contentsTask = httpClient.GetStringAsync(url);
  
              // Await! control returns to the caller and the task continues to run on another thread.
              string content = await contentsTask;
              Console.Out.WriteLine("Response Body: \r\n {0}", content);
  
              // Convert string to JSON object.
              result = Newtonsoft.Json.JsonConvert.DeserializeObject&lt;RootObject&gt; (content);
  
              // Update listview.
              RunOnUiThread (() =&gt; {
                  listView.Adapter = new CustomListAdapter(this, result.posts);
  //              progressBar.Visibility = ViewStates.Gone;
              });
          }
  
          void OnListItemClick(object sender, AdapterView.ItemClickEventArgs e)
          {
              Post item = result.posts.ElementAt (e.Position);
  
              // Passing object form one activity to other.
              Intent i = new Intent(Application.Context, typeof(FeedDetailsActivity));
              i.PutExtra("item", JsonConvert.SerializeObject(item));
              StartActivity(i);
          }
      }
  }
  </code></pre>