Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Find a TextView from a View that has been Inflated

$
0
0

In Xamarin, if I Inflate a View, how can I set the text of a TextView in the View?

Here is my code:

public class FragmentOrange : Android.Support.V4.App.Fragment
{
    string message;
    View view;

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

        view = inflater.Inflate(Resource.Layout.fragment_orange, container, false);
        return view;
    }

    public void SendMessageToFragmentGreen(string value)
    {
        message = value;
    }

    public void GetMessage(string value)
    {
        message = value;
        var textView = view.FindViewById<TextView>(Resource.Id.textViewDisplay);
        textView.Text = message;
    }

}

When I call the GetMessage function, I get the following error:

Object reference not set to an instance of an object

At this line of code:

var textView = view.FindViewById<TextView>(Resource.Id.textViewDisplay);

Here is my Layout code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:text="Text"
        android:id="@+id/textViewDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Thanks in advance


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>