MonoDroid onClick From XML No such method error

LiftInspection.axml

<Button
      android:id="@+id/expandCollapse1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"          
      android:drawableLeft="@drawable/expand_arrow"
      android:text="Function/Controls"
      android:textSize="20sp"
      android:textColor="@android:color/white"
      android:background="@drawable/expandCollapseButton"
      android:gravity="center"
      android:onClick="button_Click"/>

      

LiftInspection.cs

protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.LiftInspection);
    }

public void button_Click(View view)
    {
        Toast.MakeText(this, "Testing", ToastLength.Long).Show();           
    }

      

As soon as I press the button, the app crashes and exits. In Android Log I found "java.lang.IllegalStateException: Could not find button_Click (View) method in activity class cpecfieldapp.LiftInspection for onClick handler in view class android.widget.Button with id'expandCollapse1 '"

Everything I found when setting up a click event from xml only shows what I am doing. By putting it in android: onClick from XML and having a public void where the only parameter is the view in the action that implements that layout. What am I missing?

+3


source to share


3 answers


This is not something that is supported in the current version of Mono for Android. You can learn more about this in this bug report .



+3


source


I got this to work by adding an attribute [Export]

to your method button_Click

.



0


source


It is now possible. You need to do three things:

  • Link Mono.Android.Export

  • using Java.Interop

  • Annotate the event method like this:
[Export ("button_Click")]  
public void button_Click(View view)  
    {  
        Toast.MakeText(this, "Testing", ToastLength.Long).Show();             
    }  

      

0


source







All Articles