locked
Setting Activity Context for an Android View RRS feed

  • Question

  • User381780 posted

    I am trying to set the Activity Context for a custom renderer page. I've tried setting it a few different ways, including using the CurrentActivity plugin from James Montemagno, but every time I try to set the activity, its value remains null. This is causing an "Object Reference not set to an instance of the object" exception in my code.

    I am not having this error when building the sample Xamarin Forms custom renderer solution, and my code is the same as far as I can tell.

    activity = this.Context as Activity;
    view = activity.LayoutInflater.Inflate(Resource.Layout.Test_Layout, this, false);
    
    textureView = view.FindViewById<TextureView>(Resource.Id.textureView);
    textureView.SurfaceTextureListener = this;
    

    The error is occuring on the second line of the code where I am attempting to define the view.

    Wednesday, July 3, 2019 3:41 PM

Answers

  • User381780 posted

    Hi, it seems that my issue was with how I was defining my Activity. I needed the Activity to be of type Android.App.Activity, but it was being defined as MyApp.Droid.Activity. After specifying in the activity's declaration, it seems to be working correctly now.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, July 5, 2019 2:31 PM

All replies

  • User369979 posted

    Do you want to access the MainActivity in your page renderer? Both the constructor

    public CustomPageRenderer(Context context) : base(context)
    {
    }
    

    and this.Context should work. Could you please show your sample to help me reproduce this issue on my side?

    Thursday, July 4, 2019 6:35 AM
  • User381780 posted

    Hi, it seems that my issue was with how I was defining my Activity. I needed the Activity to be of type Android.App.Activity, but it was being defined as MyApp.Droid.Activity. After specifying in the activity's declaration, it seems to be working correctly now.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, July 5, 2019 2:31 PM
  • User369979 posted

    @zack19 Then do you mean your issue has been solved?

    Tuesday, July 9, 2019 10:14 AM
  • User381780 posted

    Yes, sorry. I have marked my comment as the solution.

    Tuesday, July 9, 2019 2:17 PM