5

I have the following layout:

<Button android:id="@+id/MyButton"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/Hello"
    android:clickable="true"
    android:onClick="Foo"
/>

And this in my Activity:

[Activity(Label = "LayoutTest", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Main);
    }

    public void Foo(View v)
    {
        Toast.MakeText(v.Context, "Bar", ToastLength.Long).Show();
    }
}

When I debug this in the emulator the app crashes when I click MyButton with the following excerpt in the log:

E/AndroidRuntime(  507): FATAL EXCEPTION: main
E/AndroidRuntime(  507): java.lang.IllegalStateException: Could not find a method    Foo(View) in the activity class helloworld.Activity1 for onClick handler on view class android.widget.Button with id 'MyButton'
E/AndroidRuntime(  507):    at android.view.View$1.onClick(View.java:2059)
E/AndroidRuntime(  507):    at android.view.View.performClick(View.java:2408)
E/AndroidRuntime(  507):    at android.view.View$PerformClick.run(View.java:8816)
E/AndroidRuntime(  507):    at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(  507):    at android.os.Handler.dispatchMessage(Handler.java:92)
Chris Hawkins
  • 808
  • 1
  • 7
  • 22

2 Answers2

8

MonoDroid does not support registering events in this way.

You need to hook up the events yourself in your activity's OnCreate.

Update: As an update, MonoDroid does now support this: http://docs.xamarin.com/guides/android/advanced_topics/java_integration_overview/working_with_jni/#_ExportAttribute_and_ExportFieldAttribute

Brad Moore
  • 316
  • 5
  • 23
jpobst
  • 9,982
  • 1
  • 29
  • 33
  • Then what would be the point of the android:onClick attribute? – Chris Hawkins Jul 17 '11 at 23:54
  • If you are writing in Java, it does as you expect. In MonoDroid, it has no implemented function. – jpobst Jul 18 '11 at 00:56
  • 1
    I see. Thanks. Would there be documentation on what MonoDroid doesn't support? – Chris Hawkins Jul 18 '11 at 01:50
  • 2
    Voting up this response. Wish I had seen this info elsewhere before spending time on this. – TonyG Sep 08 '12 at 22:44
  • 1
    As an update, MonoDroid does now support this: http://docs.xamarin.com/android/advanced_topics/Java_Integration?highlight=export#ExportAttribute_and_ExportFieldAttribute – jpobst Sep 09 '12 at 04:53
  • @jpobst I'm kind of a MonoDroid noob...how exactly would that enable you to register events as described in the question? – paz Sep 14 '12 at 14:44
  • 1
    Nevermind, i figured it out: just throw an [Export] attribute above the onClick methods and make sure to add a reference to Mono.Android.Export in your project. – paz Sep 14 '12 at 15:09
5

In addition to an [Export ("javamethodname")] attribute on the onClick methods and a reference to Mono.Android.Export, you also need

using Java.Interop;
StarsSky
  • 6,721
  • 6
  • 38
  • 63
Brian G
  • 233
  • 2
  • 6