3

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 click the button, the app crashes and forces close. In the Android Log, I find "java.lang.IllegalStateException: Could not find a method button_Click(View) in the activity class cpecfieldapp.LiftInspection for onClick handler on view class android.widget.Button with id'expandCollapse1'"

Everything I've found on setting up a click event from xml only shows what I'm doing. Putting it in android:onClick from XML and having a public void where the only parameter is a View in the activity implementing that layout. What am I missing?

jmease
  • 2,507
  • 5
  • 49
  • 89

3 Answers3

3

That's not something that's supported in the current version of Mono for Android. You can refer to this bug report for more details about it.

Greg Shackles
  • 10,009
  • 2
  • 29
  • 35
0

I have gotten this to work by adding the [Export] attribute to your button_Click method.

Alex Wiese
  • 8,142
  • 6
  • 42
  • 71
0

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

  1. Reference Mono.Android.Export
  2. using Java.Interop
  3. Annotate your event method like this:
[Export ("button_Click")]  
public void button_Click(View view)  
    {  
        Toast.MakeText(this, "Testing", ToastLength.Long).Show();             
    }  
Jannie Theunissen
  • 28,256
  • 21
  • 100
  • 127