他のActivityまたはFragmentと関連付けされているXMLのandroid:idを取得する方法

前提

MainActivity.csでfilternavigation.xmlの android:id="@+id/drawer_layout2"を取得したいです。

関連付け
MainActivity.cs - activity_main.xml
Filter.cs - filter_navigation.xml

実現したいこと

ボタン押下でナビゲーション画面表示させたく、
activity_mainにあるボタンを押下したら、filter_navigation.xmlを開くようにしたいとかんがえております。
※filter_navigation.xmlのレイアウトはDrawerLayoutにしております。

発生している問題・エラーメッセージ

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

下記コードのm_drawerがnullになっていることが原因でした。

該当のソースコード

MainActivity.cs

C#

DrawerLayout m_drawer;<中略> private void Btn_Filter_Click(object sender, EventArgs args) { //filter_navigation.xmlを開く  m_drawer.OpenDrawer(GravityCompat.Start); }

filter_navigation.xml

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"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <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/textViewSmpVer" android:layout_width="match_parent" android:layout_height="58dp" android:text="test" android:textSize="12dp" android:textColor="@color/normalTextLight" android:gravity="center"> </TextView> </LinearLayout> </com.google.android.material.navigation.NavigationView> </androidx.drawerlayout.widget.DrawerLayout>

補足情報(FW/ツールのバージョンなど)

【開発環境】
Microsoft Visual Studio 2019 Version 16.11.19
Microsoft .NET Framework Version 4.8.04084
MonoAndroid, Version=v9.0

コメントを投稿

0 コメント