Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. (View) in a parent or ancestor Context for android:onClick attribute defined on view class Button

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

Scheduled Pinned Locked Moved Android
csharpjavaasp-netandroid
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vimalsoft Pty Ltd
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • V Vimalsoft Pty Ltd

      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

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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 a View 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

      V 1 Reply Last reply
      0
      • D David Crow

        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 a View 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

        V Offline
        V Offline
        Vimalsoft Pty Ltd
        wrote on last edited by
        #3

        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

        D 1 Reply Last reply
        0
        • V Vimalsoft Pty Ltd

          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

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          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

          V 1 Reply Last reply
          0
          • D David Crow

            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

            V Offline
            V Offline
            Vimalsoft Pty Ltd
            wrote on last edited by
            #5

            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

            D 1 Reply Last reply
            0
            • V Vimalsoft Pty Ltd

              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

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              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 activity PersonalDetailsView rather than the fragment PageFragment.

              "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

              V 1 Reply Last reply
              0
              • D David Crow

                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 activity PersonalDetailsView rather than the fragment PageFragment.

                "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

                V Offline
                V Offline
                Vimalsoft Pty Ltd
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups