主活动中的片段返回null fragmentmanager.findviewbyid<>

时间:2016-09-29 07:12:50

标签: android xamarin

我创建了MainNavigationDrawerFragment

但是,当我尝试findviewbyId时,drawerFragmentdrawerlayout都会返回null,当我尝试设置抽屉时。

由于此活动是主要启动器,通过fragmentmanager.findviewbyID(),我希望它能启动片段onCreateView事件,以便我可以对片段进行充气。然后,它返回null。

任何帮助?

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);
        }
  

/////////////////////////这是   布局///////////////////////////////

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

1 个答案:

答案 0 :(得分:0)

经过一番挖掘,我解决了它。这里有2个问题。我不得不改变布局,不要使用android:name而是使用class。

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

第二部分是我必须使用supportfragmentmanager,因为我的片段使用了support.app.fragment

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