2012-04-03 75 views
3

LiftInspection.axmlMonoDroid的的onClick從XML沒有這樣的方法錯誤

<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:無法找到一個方法button_Click(視圖)在活動類cpecfieldapp.LiftInspection onClick處理器視圖類android.widget.Button與id'expandCollapse1'」

我從xml中找到的設置點擊事件的所有內容都只顯示我在做什麼。將它放在android:onClick中,並且有一個公共的void,其中唯一的參數是實現該佈局的活動中的View。我錯過了什麼?

回答

3

這並不是當前版本的Mono for Android所支持的。你可以參考this bug report瞭解更多細節。

+1

即將4.1.0 alpha版本將支持此方案。 – jonp 2012-04-03 19:04:19

+0

4.10.2中沒有工作,仍然與OP相同。 – 2014-02-07 03:08:08

0

我已經通過將[Export]屬性添加到button_Click方法中來實現此目的。

0

這現在是可能的。你需要做三件事情:

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