naffie
naffie

Reputation: 729

View class object is null inside Fragment

I have a class which implements the View class. I also have an Activity that hosts 2 Fragments. In the OnCreate method of the Fragment, I instantiate the View class and pass Activity.ApplicationContext because it takes in a context in its constructor.

I have two methods inside the Fragment. One is called in the OnCreateView and another elsewhere in the Fragment. The value of the View class object is not null inside the method called in OnCreateView. However, when I try to access the View object from the other method called elsewhere in the fragment, the app fails with a NullPointerException pointing to the View object.

I don't understand why,and yet this class is instantiated in the fragment's OnCreate method.Below is my code:

namespace Sample
{
    public class SampleFragment : Fragment
    {
        private MyView mView;
        public override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            mView = new MyView (Activity.ApplicationContext);
        }

        public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            //var root = inflater.Inflate (Resource.Layout.fragment_map, container, false);

            Populate ();

            return mView;
        }

        public void Populate ()
        {
            //mView here is not null

            List<Point> points = db.GetAllPoints ();

            mView.mSampleCalculations.ClearViewElements ();

            foreach (Point lPoint in points) {
                MViewElement element = new MViewElement ();

                element .Size = MViewElement .mSize.Custom;
                element .CustomSize = 1;
                element .Shape = MViewElement .mShape.Circle;

                mView.mSampleCalculations.AddElement (element );

            }
        }

        public void CenterViewOnCoordinates()
        {
            try{
                if(mView != null) //mView is null here
                {
                    if (mView .mSampleCalculations.GetElement ("Coordinates") != null)
                    {
                        mView.CenterView (Icoordinate);
                    }
                }
            }catch(Exception e){
                Log.Error(e.Message);
            }
        }
    }
}

Upvotes: 0

Views: 447

Answers (2)

The Original Android
The Original Android

Reputation: 6215

About code in OnCreate method, mView = new MyView (Activity.ApplicationContext); This does not help create the View object. Another issue, it is not safe to cache/save the view object with mView because views in Android may be changed at any time.

You need to override the onCreateView(), as suggested by jmateo.Except with a little change:

// using local scope for the View object
View root = inflater.Inflate (Resource.Layout.fragment_map, container, false)
Populate();
return root;

Upvotes: 1

jmateo
jmateo

Reputation: 579

I've never done Xamarin, only straight up Java but the same rules should apply. You cannot instantiate a view with the application context. You need to use the activity context and when using fragments, you need to do so in your onCreateView, not onCreate.

Your onCreateView should look similar to:

mView = inflater.Inflate (Resource.Layout.fragment_map, container, false)
Populate();
return mView;

Upvotes: 0

Related Questions