单Droid的onClick事件未找到 [英] Mono Droid onClick event not found
问题描述
我有以下布局:
<按钮机器人:ID =@ + ID /用作MyButton
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/你好
机器人:可点击=真
安卓的onClick =富
/>
这在我的活动:
【活动(标签=LayoutTest,MainLauncher = true时,图标=@绘制/图标)]
公共类活动1:活动
{
保护覆盖无效的OnCreate(束束)
{
base.OnCreate(包); 的setContentView(Resource.Layout.Main);
} 公共无效美孚(视图V)
{
Toast.MakeText(v.Context,酒吧,ToastLength.Long).Show();
}
}
当我调试这在模拟器中应用程序崩溃,当我点击用作MyButton的日志中摘录如下:
E / AndroidRuntime(507):致命异常:主要
E / AndroidRuntime(507):java.lang.IllegalStateException:找不到在活动课helloworld.Activity1为的onClick处理程序方法foo(视图)在视图类android.widget.Button id为用作MyButton
E / AndroidRuntime(507):在android.view.View $ 1.onClick(View.java:2059)
E / AndroidRuntime(507):在android.view.View.performClick(View.java:2408)
E / AndroidRuntime(507):在android.view.View $ PerformClick.run(View.java:8816)
E / AndroidRuntime(507):在android.os.Handler.handleCallback(Handler.java:587)
E / AndroidRuntime(507):在android.os.Handler.dispatchMessage(Handler.java:92)
MonoDroid的不支持这种方式注册的事件。
您需要挂接自己在事件中的活动的OnCreate。
更新:
作为一个更新,MonoDroid的现在是否支持这一点:<一href=\"http://docs.xamarin.com/guides/android/advanced_topics/java_integration_overview/working_with_jni/#_ExportAttribute_and_ExportFieldAttribute\" rel=\"nofollow\">http://docs.xamarin.com/guides/android/advanced_topics/java_integration_overview/working_with_jni/#_ExportAttribute_and_ExportFieldAttribute
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)
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
这篇关于单Droid的onClick事件未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!