Want to do
I want to get android:id = "@ id/drawer_layout2" at activity which is not linked with SetContentView because I want to open navigation? DrawerLayout? with a button. I already know that drawer.OpenDrawer(GravityCompat.Start) open it but drawer is null.
Relation
MainActivity.cs - activity_main.xml
NaviDrawer.cs - drawer.xml
Error Message
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Code
MainActivity
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>
CodePudding user response:
You may directly get the current drawer inside a view/fragment so that you won't get a NullPointerException,such like this:
m_drawer = (DrawerLayout) getActivity().findViewById(Resource.Id.drawer_layout2);
And then in your Button click event you may try to set like this:
private void Btn_Drawer_Click(object sender, EventArgs args)
{
m_drawer.OpenDrawer(m_drawer);
}
These are just my suggestions and I hope they will help you. If there are follow-up questions, I may follow up.
CodePudding user response:
Linked file
ActivityMain.cs - activity_main.xml
FragmentEvent.cs - fragment_event.xml
(At this time, I access to drawerlayout's ID in activity_main from FragmentEvent)
Added Source
((ActivityMain)Activity).m_drawer.OpenDrawer(GravityCompat.Start);
Fixed Source
I change m_drawer from private to public
ActivityMain.cs
public class MainActivity : AppCompatActivity, NavigationView.IOnNavigationItemSelectedListener, TabLayout.IOnTabSelectedListener
{
private Context context;
//DrawerLayout m_drawer;
public DrawerLayout m_drawer;