4

In 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>
In 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" />

In 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)

In 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>

4
  • Change the first argument in the ArrayAdapter constructor call to this.Activity. Commented Apr 16, 2016 at 1:17
  • Assuming the code you pasted is not complete, missing the return of the View. Commented Apr 16, 2016 at 1:18
  • I got that problem Object reference not set to an instance of an object Commented Apr 16, 2016 at 1:51
  • @J.Joe Sounds like you are trying to access a method of a class instance using a null reference(?) Can you provide more details (update your answer if needed) on which line, stack trace, etc, ... Commented Apr 16, 2016 at 2:00

2 Answers 2

1

Reason for your first Build Error is as explained by SushiHangover, i.e You should be passing an instance of the parent Activity (activity which hosts this fragment) as the first parameter to ArrayAdapter Constructor.

Reason for your run-time (Object reference not set to an instance of an object) Exception is because "Activity" property is null. "Activity" property will have a value only when the Fragment attachment to parent Activity is complete. So essentially, you should not be accessing Activity property from OnCreateView Method. You can initialize your listview either in OnAttach or OnCreate callbacks. I am modifying your code accordingly.

`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;
}
}
Sign up to request clarification or add additional context in comments.

Comments

1
  • Passing the Activity of your Fragment as the first arg of the Widget's ArrayAdapter
  • Added a return of your new view;

Code:

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);
    lst.SetAdapter(new ArrayAdapter<string> (this.Activity,Resource.Layout.abc_list_menu_item_layout,Resource.Menu.Genres));
    //Event click on listview
    lst.ItemClick+= delegate(object sender, AdapterView.ItemClickEventArgs e) {             
    };
    return view;
}

8 Comments

I got that problem Object reference not set to an instance of an object
lst.SetAdapter(new ArrayAdapter<string> (this.Activity,Resource.Layout.abc_list_menu_item_layout,Resource.Menu.Genres));
Are any of those objects' null? Just set a break point there and debug that line.
lst.SetAdapter(new ArrayAdapter<string> (this.Activity,Resource.Layout.abc_list_menu_item_layout,Resource.Menu.Genres)); it get null
App be break there
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.