Android Studio 从未链接的活动获取XML的ID

gmxoilav  于 2022-11-16  发布在  Android
关注(0)|答案(2)|浏览(176)

"想做什么"
我想在未与SetContentView链接的活动中获取android:id =“@+id/drawer_layout2”,因为我想使用按钮打开导航?DrawerLayout?。我已经知道drawer.OpenDrawer(GravityCompat.Start),但drawer为空。

关系

主要活动. cs-活动_主要. xml
NaviDrawer.cs -绘图文件. xml

错误消息

System.NullReferenceException: 'Object reference not set to an instance of an object.'

代码

主要活动

public class MainActivity : AppCompatActivity, NavigationView.IOnNavigationItemSelectedListener, TabLayout.IOnTabSelectedListener
{
    private Context context;
    DrawerLayout m_drawer;

protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        SetContentView(Resource.Layout.activity_main);
        context = this;

        m_drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout2);

        btn_drawer = FindViewById<Button>(Resource.Id.btn_drawer);
        btn_drawer.Click += Btn_Drawer_Click;
    }

    private void Btn_Drawer_Click(object sender, EventArgs args)
    {
        m_drawer.OpenDrawer(GravityCompat.Start);
    }

Drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 
 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:id="@+id/drawer_layout2"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:fitsSystemWindows="true"
                                    tools:openDrawer="start">

  <com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/test"
            android:layout_width="match_parent"
            android:layout_height="58dp"
            android:text="test"
            android:textSize="12dp"
            android:gravity="center">
        </TextView>
    </LinearLayout>

  </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>
bf1o4zei

bf1o4zei1#

您可以直接在视图/片段中获取当前抽屉,这样就不会出现NullPointerException,例如:

m_drawer = (DrawerLayout) getActivity().findViewById(Resource.Id.drawer_layout2);

然后在你的Button click事件中你可以尝试这样设置:

private void Btn_Drawer_Click(object sender, EventArgs args)
{
    m_drawer.OpenDrawer(m_drawer);
}

这些只是我的建议,希望能对大家有所帮助,如果有跟进的问题,我会跟进的。

cygmwpex

cygmwpex2#

链接文件

活动主文件. cs-活动主文件. xml
文件名:
(At这次,我从FragmentEvent访问activity_main中drawerlayout的ID)

添加的源

((ActivityMain)Activity).m_drawer.OpenDrawer(GravityCompat.Start);

固定来源

我将m_drawer从私有更改为公共
ActivityMain.cs

public class MainActivity : AppCompatActivity, NavigationView.IOnNavigationItemSelectedListener, TabLayout.IOnTabSelectedListener
{
    private Context context;
    //DrawerLayout m_drawer;
    public DrawerLayout m_drawer;

相关问题