【问题标题】:MonoDroid onClick From XML No Such Method ErrorMonoDroid onClick From XML 没有这样的方法错误
【发布时间】:2012-04-17 08:18:55
【问题描述】:

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();           
    }

单击按钮后,应用程序立即崩溃并强制关闭。在 Android 日志中,我发现“java.lang.IllegalStateException:在 id'expandCollapse1' 的视图类 android.widget.Button 上的 onClick 处理程序的活动类 cpecfieldapp.LiftInspection 中找不到方法 button_Click(View)”

我在从 xml 设置点击事件时发现的所有内容都只显示了我在做什么。将其放入 XML 中的 android:onClick 并具有公共 void,其中唯一的参数是实现该布局的 Activity 中的 View。我错过了什么?

【问题讨论】:

    标签: android xamarin.android


    【解决方案1】:

    当前版本的 Mono for Android 不支持该功能。更多详情可以参考this bug report

    【讨论】:

    • 即将发布的 4.1.0 alpha 版本将支持这种情况。
    【解决方案2】:

    我已经通过将[Export] 属性添加到您的button_Click 方法来实现此功能。

    【讨论】:

      【解决方案3】:

      这现在是可能的。你需要做三件事:

      1. 参考Mono.Android.Export
      2. using Java.Interop
      3. 像这样注释您的事件方法:
      [Export ("button_Click")]  
      public void button_Click(View view)  
          {  
              Toast.MakeText(this, "Testing", ToastLength.Long).Show();             
          }  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-14
        • 2017-10-02
        • 2017-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-01
        相关资源
        最近更新 更多