1

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

I am wanting to code an application where I can call methods in different fragments to pass values between fragments and display these values.

Here is my MainActivity code:

public class MainActivity : FragmentActivity
{
    string message;

    FragmentOrange fragOrange;
    FragmentGreen fragGreen;
    FragmentRed fragRed;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        SetContentView (Resource.Layout.activity_main);

        fragOrange = new FragmentOrange ();
        fragGreen = new FragmentGreen ();
        fragRed = new FragmentRed ();

        List<Android.Support.V4.App.Fragment> fragments = new List<Android.Support.V4.App.Fragment>();
        fragments.Add(fragOrange);
        fragments.Add(fragGreen);
        fragments.Add(fragRed);

        MyPagerAdapter pageAdapter = new MyPagerAdapter(SupportFragmentManager, fragments);
        var pager = FindViewById<ViewPager>(Resource.Id.myViewPager);
        pager.Adapter = pageAdapter;

        pager.SetCurrentItem (0, true);
        fragOrange.SetMessage ("Fragment orange");
    }
}

Here is my FragmentOrange code:

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

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

        view = inflater.Inflate(Resource.Layout.fragment_orange, container, false);
        textViewDisplay = view.FindViewById<TextView>(Resource.Id.textViewDisplay);
        return view;
    }

    public void SetMessage(string value)
    {
        message = value;
        textViewDisplay.Text = value;
    }
}

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

Object reference not set to an instance of an object

At this line of code:

textViewDisplay.Text = value;

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

Garry
  • 1,251
  • 9
  • 25
  • 45

4 Answers4

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

should be

TextView textview = (TextView) findViewById(R.id.textViewDisplay);

please note, that you cannot update a textview from an activity without a handler, as it is a part of the UI.

//before oncreate
private int i;
Handler handler;

//oncreate
i = 0;
        handler = new Handler();
        handler.post(mUpdate);

//before the last closing bracket in your activity
     Runnable mUpdate = new Runnable(){
            public void run() {
                textview.setText("your string here");

                i++;

                handler.postDelayed(this, 1000);
            }
          };
Technivorous
  • 1,682
  • 2
  • 16
  • 22
0
TextView textView = view.FindViewById<TextView>(Resource.Id.textViewDisplay);

Add this line below after inflating the view. I mean in OncreateView.

tharkay
  • 5,913
  • 2
  • 26
  • 33
anjaneya
  • 708
  • 9
  • 11
0

Change this line

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

to this:

    TextView textView = (TextView)view.findViewById(R.id.textViewDisplay);

Bw you should define and inflate your view like this:

    LinearLayout view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        view = (LinearLayout) inflater.inflate(
        R.layout.fragment_orange, container, false);

        return view;
    }  
Sayed Jalil Hassan
  • 2,535
  • 7
  • 30
  • 42
0

Try that:

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

Change to:

TextView textview = (TextView) view.findViewById(R.id.textViewDisplay);

then

view.text.setText(message);
Engr Waseem Arain
  • 1,163
  • 1
  • 17
  • 36