coderocks coderocks - 2 months ago 21
Android Question

Fragment in main activity returns null fragmentmanager.findviewbyid<>

I have created the

MainNavigationDrawerFragment
.

However, when I try to
findviewbyId
, both the
drawerFragment
and
drawerlayout
returns
null
, when I try to set up drawer.

Since this activity is the main launcher, through the
fragmentmanager.findviewbyID()
, I would expect it to kick off the fragment
onCreateView
event so that I can inflate the fragment. Howerver, it's returning null.

Any help?

public class ProfileView1 : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Create your application here
SetContentView(Resource.Layout.ProfileView2);
// other code....

// Navigation Drawer
SetUpDrawer();
}

private void SetUpDrawer()
{
**MainNavigationDrawerFragment drawerFragment =
FragmentManager.FindFragmentById<MainNavigationDrawerFragment>
(Resource.Id.nav_drwr_fragment);

DrawerLayout drawerLayout = FindViewById<DrawerLayout>**(Resource.Id.drawer_layout);**
drawerFragment.SetUpDrawer(Resource.Id.nav_drwr_fragment, drawerLayout, toolBar);
}



/////////////////////////This is the
layout///////////////////////////////


<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/ToolBarOnlyLayout"
android:id="@+id/toolbarProfile" />
<include
layout="@layout/ProfileMainPageLayout"
android:id="@+id/profileMainPageCardView" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recentSearchRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

<fragment
android:id="@+id/nav_drwr_fragment"
android:name="UserProfile.Fragments.MainNavigationDrawerFragment"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/navigationdrawer_main_fragment"
tools:layout="@layout/navigationdrawer_main_fragment"/>
</android.support.v4.widget.DrawerLayout>

Answer

After some digging, I solved it. There are 2 issues here. I had to change the layout to not use android:name but class instead.

<fragment
        android:id="@+id/nav_drwr_fragment"
        *class="UserProfile.Fragments.MainNavigationDrawerFragment"*
        android:layout_width="@dimen/nav_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/navigationdrawer_main_fragment"
        tools:layout="@layout/navigationdrawer_main_fragment"/>

The second part is that i had to use supportfragmentmanager since my fragment uses support.app.fragment

 drawerFragment = (MainNavigationDrawerFragment)this.SupportFragmentManager.FindFragmentById(Resource.Id.nav_drwr_fragment);