0

This is my xml file

<LinearLayout
    android:orientation="vertical"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="40dp"
    android:id="@+id/MainPersonalIDLayout">
    <TextView
        android:text="Personal ID"
        android:textSize="18dp"
        android:textColor="#000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:id="@+id/PersonalIDTitle" />
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:id="@+id/SubPersonalIDLayout">
        <TextView
            android:text="No ID"
            android:textSize="20dp"
            android:gravity="center"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:id="@+id/PersonalID" />
        <Button
            android:text="Use this"
            android:textAllCaps="false"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="UsePersonalID"
            android:id="@+id/UsePersonalID" />
    </LinearLayout>
</LinearLayout>

This is my Dashboard Fragment

public class DashboardTab : Fragment
{
    View DashboardView;
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        // return inflater.Inflate(Resource.Layout.YourFragment, container, false);

        DashboardView = inflater.Inflate(Resource.Layout.Dashboard, container, false);
        return DashboardView;
    }
}

And this is my MainActivity.cs, I created an instance of the Dashboard fragment

SetContentView(Resource.Layout.activity_main);
DashboardTab Dashboard = new DashboardTab();
TextView PersonalIDHolder = Dashboard.View.FindViewById<TextView>(Resource.Id.PersonalID);

This is the error that shows when I run System.NullReferenceException: 'Object reference not set to an instance of an object.'

Is it possible to get the view from an instance of Fragment?

Android get view of fragment in activity

Thank you

Tuna
  • 22
  • 4

2 Answers2

0

The answer is yes.I don't know how you get instance of your fragment, but there is a way to get a Fragment by FindFragmentByTag.

When you add the Fragment,you could do something like:

 SupportFragmentManager
            .BeginTransaction()
            .Add(Resource.Id.settingsContainer, new DashboardTab() , "DashboardTab") 
            .Commit();

then get the DashboardTab fragemnt in Activity OnResume() or OnStart() method:

protected override void OnResume()
    {
        base.OnResume();
        DashboardTab Dashboard = SupportFragmentManager.FindFragmentByTag("DashboardTab");
        TextView textView =  Dashboard.View.FindViewById<TextView>(Resource.Id.PersonalID);
    }

  
Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
  • oh sorry I forgot to include it, DashboardTab Dashboard; Dashboard = new DashboardTab(); – Tuna Nov 09 '20 at 03:06
  • So I need to get it from SupportFragmentManager? – Tuna Nov 09 '20 at 03:37
  • If you use `DashboardTab Dashboard; Dashboard = new DashboardTab(); `,it also could work.If you use `SupportFragmentManager.FindFragmentByTag` ,it will ensures that you don't have to` new DashboardTab()` repeatedly on other pages. – Leo Zhu Nov 09 '20 at 03:42
  • oh, so I just need to add all my fragments add the beginning then just replace them depending on what fragment I need? – Tuna Nov 09 '20 at 03:56
  • No,you don't need to add all fragments,i mean that after you add the fragment and define a tag for it,you could get it when you want by its tag,avoid to call `new YourFragment();` – Leo Zhu Nov 09 '20 at 05:05
  • It shows error, did I do something wrong? https://snipboard.io/zqfarv.jpg – Tuna Nov 09 '20 at 09:53
  • Thank you for sharing this to me tho, it helped me – Tuna Nov 09 '20 at 15:03
  • @DeathAscend Because the fragment isn't fully rendered to your Activity when you fetch a TextView in onCreate() method in activity,you just move `DashboardTab Dashboard = SupportFragmentManager.FindFragmentByTag("DashboardTab"); TextView textView = Dashboard.View.FindViewById(Resource.Id.PersonalID);` to `OnResume()` or `OnStart()` method.Although that answer is true, it doesn't fit your question, it's just a way we normally declare a TextView in our fragment, not a way we get a TextView in our Activity. – Leo Zhu Nov 10 '20 at 01:30
0

Yes, You will get the System.NullReferenceException because you are trying to access the Fragment's View in onCreate Method of activity and till now fragment view is not initialized.

Update your views in OnCreateView method in Fragment

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {

            var DashboardView = inflater.Inflate(Resource.Layout.Dashboard, container, false);
TextView textView = View.FindViewById<TextView>(Resource.Id.PersonalID);
            return DashboardView;
        }

OR If your changes depend on Activity your fragment is attached to, then you can use OnActivityCreated method of Fragment.

public override void OnActivityCreated(Bundle savedInstanceState)
        {
            base.OnActivityCreated(savedInstanceState);
            TextView textView = View.FindViewById<TextView>(Resource.Id.PersonID);

            var act = (MainActivity)Activity;
        }
Jai Gupta
  • 1,374
  • 9
  • 8
  • oh, so that's why it always show Object reference not set to an instance of an object, Thank you – Tuna Nov 09 '20 at 15:04