Share via


(View) in a parent or ancestor Context for android:onClick attribute defined on view class Button

Question

Wednesday, August 30, 2017 5:31 PM

Good Day Everyone

I have an activity class defined like this

Hide Copy Code [Activity(Label = "ActivityHome", MainLauncher = false, Icon = "@drawable/lenderlogo", Theme = "@style/MyTheme")] public class ActivityHome : ActionBarActivity //AppCompatActivity {

and the activity is hosting a Tab that display its content from a fragment. A fragment is defined separately and it has a button that is defined like this

Hide Copy Code

as you can see i have a click event that is triggered when the button is clicked. The code to handle the event of the button is defined in the fragment class file like this

Hide Copy Code //Find Controls to hook Button btndetailssave = FindViewById(Resource.Id.btndetailssave);

            // set onclick listener here, by deleting some process
            btn_details_save.Click += delegate
            {
                btn_details_saveClick();
            };

and he function itself is defined like this

Hide Copy Code public void btndetailssaveClick() { //Do stuff } Now when i run the application, the button is click the button the following error occurs

AndroidRuntime(19059): java.lang.IllegalStateException: Could not find method btndetailssaveClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class Button with id 'btndetailssave'

Please note that this is C# code , which means i am using xamarin ,but the error is Android related not Xamarin.

Thanks

All replies (2)

Sunday, September 3, 2017 9:05 PM âś…Answered

The issue was that my class was not inheriting from a Fragment Class now its fine. Resolved


Thursday, August 31, 2017 1:23 PM

Your method public void btndetailssaveClick() { //Do stuff }
should be written in fragment class not in parent class.