Search code examples
androidnulltextviewxamarinandroid-viewgroup

Xamarin Android TextView returning null


Despite much searching i can't find the answer to what is probably a very simple problem.

I have a method that creates a pop up window. I then need to access the TextViews of the pop up window's XML and change the text. The problem is my textView is returning null and therefore giving me a null ref exception when i try to set the text in the method that is called at the bottom, countDown();

This is being run in an activity, not a fragment. I understand the principles of null ref exceptions but can't work out what's happening here or how to fix it.

Here is the code for the method:

public void clockPopup()
   {

    LayoutInflater inflater = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);

    View myPopup = inflater.Inflate (Resource.Layout.PopUpClockTimer, null); 

    offerExpired = FindViewById<TextView> (Resource.Id.offer_expired); //returns null
    timerDigit = FindViewById<TextView> (Resource.Id.test_timer); //returns null

    PopupWindow popup = new PopupWindow (myPopup, ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent, true);
    popup.ShowAtLocation (myPopup, GravityFlags.Center, 0,0);
    popup.Update ();


    RunOnUiThread (() => {
        countDown ();
    });


    }

here is the xml for the TextViews i'm trying to access

 <TextView
         android:id="@+id/test_timer"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:textSize="30sp"/>
 <TextView
         android:id="@+id/offer_expired"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:textSize="30sp"/>

Any help appreciated.


Solution

  • Your FindViewById is searching in the wrong parent. This should do the trick:

    myPopup.FindViewById<TextView>