我只是想将我在原生Android / Java中编写的一些代码移植到MonoDroid中 - 但是当我点击一个按钮时出现以下错误:
java.lang.IllegalStateException:在活动类icantalk.android.CreateProfile中找不到方法DoClick(View),用于视图类android.widget.Button上的onClick处理程序,其id为'createProfilePicBtn'
public void DoClick(View view)
{
switch (view.Id)
{
case Resource.Id.createProfilePicBtn:
{
Log.Error("Profile Pic", "Clicked");
break;
}
case Resource.Id.createProfileSbmtBtn:
{
Log.Error("Save Button", "Clicked");
break;
}
}
}
我的布局xml的相关部分是:
<Button
android:id="@+id/createProfilePicBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="10dip"
android:onClick="DoClick"
android:text="@string/createProfileImgBtnTxt" />
<Button
android:id="@+id/createProfileSbmtBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="10dip"
android:onClick="DoClick"
android:text="@string/createProfileSaveBtnTxt" />
答案 0 :(得分:1)
MonoDroid目前不支持以这种方式注册事件。
您可以使用以下代码注册活动:
public override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
//Do other oncreate stuff here (including SetContentView etc)
//Register the event for your first button
Button btn = FindViewById<Button>(Resource.id.createProfilePicBtn);
btn.Click += DoClick;
//Register the event for your second button
Button btn2 = FindViewById<Button>(Resource.id.createProfileSbmtBtn);
btn2.Click += DoClick;
}
public void DoClick(object sender, EventArgs e)
{
View view = (View)sender;
switch (view.Id)
{
case Resource.Id.createProfilePicBtn:
{
Log.Error("Profile Pic", "Clicked");
break;
}
case Resource.Id.createProfileSbmtBtn:
{
Log.Error("Save Button", "Clicked");
break;
}
}
}