Custom View Not Working In AppCompatActivity

Nathan Sokalski 4,126 Reputation points
2022-11-29T06:25:03.517+00:00

I have a Xamarin.Android Class Library which contains a Custom View that inherits from GridLayout. I have used this Custom View many times with projects that inherit from Activity, but in my projects that inherit from AppCompatActivity I receive errors if attempting to use this Custom View. The error occurs on the following line in the OnCreate method of MainActivity:

this.SetContentView(Resource.Layout.activity_main);  

The output window gives the following:

**Java.Lang.RuntimeException:** 'Binary XML file line #1 in com.companyname.itsabouttimeexperimental:layout/activity_main: Binary XML file line #1 in com.companyname.itsabouttimeexperimental:layout/mainscreenlayout: Error inflating class XamarinAndroidAppExtras.CircleButton'  

(NOTE: activity_main is my main layout which uses an include that references mainscreenlayout, which is a GridLayout in a separate xml file, and XamarinAndroidAppExtras.CircleButton is my Custom View). The designer in Visual Studio 2022 displays my Custom View perfectly, it is not until I run the debugger that the error occurs. How can I figure out what needs changed in my Custom View to make it work in an AppCompatActivity?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Nathan Sokalski 4,126 Reputation points
    2022-11-30T03:36:00.04+00:00

    I finally found the answer, and as in many cases, it was simple human error. When declaring the View in MainActivity, I was declaring it as a Button rather than the type of my Custom View (CircleButton). I appreciate your quick response, thanks!