无法将xml数据解析为xamarin android上的listview

在menu / Genre.xml中:

<?xml version="1.0" encoding="UTF-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <group android:checkableBehavior="single">
    <item
      android:id="@+id/nav_home"
      android:icon="@drawable/ic_home_black_48dp"
      android:title="Home" />
    <item
      android:id="@+id/nav_genre"
      android:icon="@drawable/ic_toc_black_48dp"
      android:title="Genres" />
   
    <item
      android:id="@+id/nav_download"
      android:icon="@drawable/ic_get_app_black_48dp"
      android:title="Download" />
  </group>
  <item android:title="Account">
  	<menu>
		<group android:checkableBehavior="single">
			<item 
  			android:id="@+id/nav_about"
  			android:icon="@drawable/ic_lock_open_black_48dp"
  			android:title="About"/>
  			<item 
  			android:id="@+id/nav_signout"
  			android:icon="@drawable/ic_perm_identity_black_48dp"
  			android:title="Sign out"/>
		</group>
  		
  	</menu>
  </item>
 
</menu>

在layout / GenreFragment.axml中

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lst_genre"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />

在viewModel / GenreFragmentVM中:

public class GenerFragmentVM : Fragment
	{
		public override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);

			// Create your fragment here
		}
		ListView lst;
		public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
		{
			// Use this to return your custom view for this Fragment
			// return inflater.Inflate(Resource.Layout.YourFragment, container, false);

			base.OnCreateView (inflater, container, savedInstanceState);
			var view = inflater.Inflate (Resource.Layout.abc_list_menu_item_layout, container, false);
			lst = view.FindViewById<ListView> (Resource.Id.lst_genre);
			lst.SetAdapter(new ArrayAdapter<string> (this,Resource.Layout.abc_list_menu_item_layout,Resource.Menu.Genres));

		

			//Event click on listview
			lst.ItemClick+= delegate(object sender, AdapterView.ItemClickEventArgs e) {
				
			};
		}
	}

Error: (19,19): Error CS1502: The best overloaded method match for
`Android.Widget.ArrayAdapter.ArrayAdapter(Android.Content.Context,
int, int)’ has some invalid arguments (CS1502) (SoundCloud)

(45,45): Error CS1503: Argument #1' cannot convert
SoundCloud.GenerFragment’ expression to type
`Android.Content.Context’ (CS1503) (SoundCloud)

在Mainactivity.cs中

protected override void OnCreate (Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);

        // Set our view from the "main" layout resource
        //ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
        SetContentView (Resource.Layout.Main);


        var mToolbar= FindViewById<Toolbar> (Resource.Id.toolbar);
        //Toolbar will now take on default action bar chacracteritics
        SetActionBar(mToolbar);
        ActionBar.Title = "home";




        //Enable suppport action bar to display hamburger
        //ActionBar.SetHomeAsUpIndicator(Resource.Drawable.icon_hambuger);
        //ActionBar.SetDisplayHomeAsUpEnabled (true);

        //Set menu hambuger
        ActionBar.SetHomeAsUpIndicator (Resource.Drawable.ic_menu_white_24dp);

        ActionBar.SetDisplayHomeAsUpEnabled (true);

        drawerLayout = FindViewById<DrawerLayout> (Resource.Id.drawer_layout);
        navigationView = FindViewById<NavigationView> (Resource.Id.nav_view);

        //Event click on navigatonView
        // 
        CreateFragments();
        LoadInditialFragment ();

        navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;




    }

    void NavigationView_NavigationItemSelected (object sender, NavigationView.NavigationItemSelectedEventArgs e)
    {
        e.MenuItem.SetChecked (true);
        if (e.MenuItem.ItemId != currentFragmentId) {
            SwitchFragment (e.MenuItem.ItemId);
        }
        drawerLayout.CloseDrawers ();
    }

    // Khoi tao gia tri cho fragment layout

    private void CreateFragments()
    {
        explorFragment= new ExploreFragment();
        genreFragment = new GenerFragment ();

    }
    //
    private void LoadInditialFragment ()
    {
        var transaction = FragmentManager.BeginTransaction ();
        transaction.Add (Resource.Id.fragmentContainer, explorFragment).Hide (explorFragment);
        transaction.Add (Resource.Id.fragmentContainer, genreFragment);
        transaction.Commit ();
    }

    private void SwitchFragment(int FragmentId)
    {
        var transaction = FragmentManager.BeginTransaction ();

        switch (currentFragmentId) {
        case Resource.Id.nav_home:
            transaction.Hide (explorFragment).Commit ();
            break;
        case Resource.Id.nav_genre:
            transaction.Hide (genreFragment).Commit ();
            break;
        }

        transaction = FragmentManager.BeginTransaction ();
        switch (FragmentId) {
        case Resource.Id.nav_home:
            transaction.Show (explorFragment);
            transaction.Commit ();
            break;

        case Resource.Id.nav_genre:
            transaction.Show (genreFragment);
            transaction.Commit ();
            break;
        }

        currentFragmentId = FragmentId;
    }
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
<!-- your content layout -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:titleTextColor="@android:color/background_light" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <FrameLayout
                android:id="@+id/fragmentContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </RelativeLayout>
    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:id="@+id/nav_view"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/navmenu" />
</android.support.v4.widget.DrawerLayout>

最佳答案 您的第一个构建错误的原因如SushiHangover所解释,即您应该将父Activity的实例(承载此片段的活动)作为ArrayAdapter构造函数的第一个参数传递.

运行时的原因(对象引用未设置为对象的实例)异常是因为“Activity”属性为null.仅当父Activity的Fragment附件完成时,“Activity”属性才会有值.基本上,您不应该从OnCreateView方法访问Activity属性.您可以在OnAttach或OnCreate回调中初始化列表视图.我正在修改你的代码.

`public class GenerFragmentVM : Fragment
{
    ListView lst;
    public override void OnCreate (Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);

         lst.SetAdapter(new ArrayAdapter<string> (this.Activity,Resource.Layout.abc_list_menu_item_layout,Resource.Menu.Genres));
    }

public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);
var view = inflater.Inflate (Resource.Layout.abc_list_menu_item_layout, container, false);
lst = view.FindViewById<ListView> (Resource.Id.lst_genre);

//Event click on listview
lst.ItemClick+= delegate(object sender, AdapterView.ItemClickEventArgs e) {             
};
return view;
}
}
点赞