[英]Mono Droid onClick event not found
我有以下布局:
<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"
/>
這在我的活動中:
[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();
}
}
當我在模擬器中調試它時,當我單擊 MyButton 時應用程序崩潰,日志中包含以下摘錄:
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)
MonoDroid 不支持以這種方式注冊事件。
您需要自己在活動的 OnCreate 中連接事件。
更新:作為更新,MonoDroid 現在支持此功能: http://docs.xamarin.com/guides/android/advanced_topics/java_integration_overview/working_with_jni/#_ExportAttribute_and_ExportFieldAttribute
除了 onClick 方法上的[Export ("javamethodname")]
屬性和對onClick
的引用Mono.Android.Export
,您還需要
using Java.Interop;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.