(View) in a parent or ancestor Context for android:onClick attribute defined on view class Button
-
Good Day Everyone I have an activity class defined like this
[Activity(Label = "Activity_Home", MainLauncher = false, Icon = "@drawable/lenderlogo", Theme = "@style/MyTheme")]
public class Activity_Home : 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
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
//Find Controls to hook
Button btn_details_save = FindViewById(Resource.Id.btn_details_save);// set onclick listener here, by deleting some process btn\_details\_save.Click += delegate { btn\_details\_saveClick(); };
and he function itself is defined like this
public void btn_details_saveClick()
{
//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 btn_details_saveClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class Button with id 'btn_details_save' Please note that this is C# code , which means i am using xamarin ,but the error is Android related not Xamarin. Thanks
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vimalsoft.com vuyiswa[at]vimalsoft.com
-
Good Day Everyone I have an activity class defined like this
[Activity(Label = "Activity_Home", MainLauncher = false, Icon = "@drawable/lenderlogo", Theme = "@style/MyTheme")]
public class Activity_Home : 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
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
//Find Controls to hook
Button btn_details_save = FindViewById(Resource.Id.btn_details_save);// set onclick listener here, by deleting some process btn\_details\_save.Click += delegate { btn\_details\_saveClick(); };
and he function itself is defined like this
public void btn_details_saveClick()
{
//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 btn_details_saveClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class Button with id 'btn_details_save' Please note that this is C# code , which means i am using xamarin ,but the error is Android related not Xamarin. Thanks
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vimalsoft.com vuyiswa[at]vimalsoft.com
My first concern is that you are declaring the click handler twice: once in the layout file, and again in code. Use one or the other, but not both. Second, you need to change
btn_details_saveClick()
to accept aView
parameter. This is what the error message is actually telling you."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
My first concern is that you are declaring the click handler twice: once in the layout file, and again in code. Use one or the other, but not both. Second, you need to change
btn_details_saveClick()
to accept aView
parameter. This is what the error message is actually telling you."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
Thanks for your reply David "My first concern is that you are declaring the click handler twice: once in the layout file, and again in code. Use one or the other, but not both." I thought we have to add the click event on the layout file and add hookup the event to the button also in the initialization of the Activity. i learned, thanks. i am coming from asp.net background i have changed my function in my frament page to be like this
public void btn_details_saveClick(View view)
{had //// set onclick listener here, by deleting some process //btn_details_save.Click += delegate //{ // btn_details_saveClick(); //}; and i ran it and i got the Error java.lang.IllegalStateException: Could not find a method btn_details_saveClick(View) in the activity class android.support.v7.widget.TintContextWrapper for onClick handler on view class android.support.v7.widget.AppCompatButton with id 'btn_details_save' i have also have a example project( 9mb) on this link Example Project To Reproduce the Error 1) Open the drawer 2) Click on loan application and click the button "Continue" and you will get an Error Thanks
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vimalsoft.com vuyiswa[at]vimalsoft.com
-
Thanks for your reply David "My first concern is that you are declaring the click handler twice: once in the layout file, and again in code. Use one or the other, but not both." I thought we have to add the click event on the layout file and add hookup the event to the button also in the initialization of the Activity. i learned, thanks. i am coming from asp.net background i have changed my function in my frament page to be like this
public void btn_details_saveClick(View view)
{had //// set onclick listener here, by deleting some process //btn_details_save.Click += delegate //{ // btn_details_saveClick(); //}; and i ran it and i got the Error java.lang.IllegalStateException: Could not find a method btn_details_saveClick(View) in the activity class android.support.v7.widget.TintContextWrapper for onClick handler on view class android.support.v7.widget.AppCompatButton with id 'btn_details_save' i have also have a example project( 9mb) on this link Example Project To Reproduce the Error 1) Open the drawer 2) Click on loan application and click the button "Continue" and you will get an Error Thanks
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vimalsoft.com vuyiswa[at]vimalsoft.com
Does the
btn_details_saveClick()
method belong to the same class that 'owns' the layout?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Does the
btn_details_saveClick()
method belong to the same class that 'owns' the layout?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
Hi David Yes it does , i have an example Project (9mb) http://www.3cx.vimalsoft.com/Example\_Android\_Exception/App10.zip
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vimalsoft.com vuyiswa[at]vimalsoft.com
-
Hi David Yes it does , i have an example Project (9mb) http://www.3cx.vimalsoft.com/Example\_Android\_Exception/App10.zip
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vimalsoft.com vuyiswa[at]vimalsoft.com
If you are defining the click-handler in the .xml file, the click-handler must be implemented in the activity not the fragment. If you are defining the click-handler in the .cs file, the click-handler can be implemented in the fragment. You'll notice in that example that the
btn_details_saveClick()
method is contained within the activityPersonalDetailsView
rather than the fragmentPageFragment
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
If you are defining the click-handler in the .xml file, the click-handler must be implemented in the activity not the fragment. If you are defining the click-handler in the .cs file, the click-handler can be implemented in the fragment. You'll notice in that example that the
btn_details_saveClick()
method is contained within the activityPersonalDetailsView
rather than the fragmentPageFragment
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
Thank you David i fixed the issue after making the PersonalDetailsView inherit from Fragment, all started to work.
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vimalsoft.com vuyiswa[at]vimalsoft.com