Xamarin.Android单击按钮事件正在给出异常,我该如何解决它



当我尝试运行模拟器时,计算按钮上发生异常。单击 += 委托,我正在尝试制作一个提示计算器,因此当用户单击按钮时,应用程序开始计算。

例外说System.NullReferenceException 已被抛出 对象引用未设置为对象的实例

我尝试了很多东西

Button calculateButton;
protected override void OnCreate(Bundle bundle)
{
   base.OnCreate(bundle);
   SetContentView(Resource.Layout.activity_main);
   calculateButton = FindViewById<Button>(Resource.Id.calculateButton);
   calculateButton.Click += delegate
            {
                "insert calculations"
            };
        }

我也试过

Button calculateButton;
protected override void OnCreate(Bundle bundle)
{
   base.OnCreate(bundle);
   SetContentView(Resource.Layout.activity_main);
   calculateButton = FindViewById<Button>(Resource.Id.calculateButton);
   calculateButton.Click += OnCalculateClick;
   void OnCalculateClick (object sender, EventArgs e)
            {
                "insert calculations"
            };
        }

我期望的是,当我单击按钮时,计算将开始

编辑:这里是activity_main.axml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minWidth="25px"
        android:minHeight="25px"
        android:id="@+id/linearLayout1">
    </LinearLayout >
    <include
        layout="@layout/content_main" />
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>

这是 content_main.axml,我按照教程所说的在此处添加了按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android        ="http://schemas.android.com/apk/res/android"
    android:orientation  ="vertical"
    android:layout_width ="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:imeActionId="@+id/inputBill"
        android:layout_width ="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText1"
        android:layout_marginBottom="0.0dp"
        android:layout_marginTop="0.0dp" />
    <Button
        android:imeActionId="@+id/calculateButton"
        android:layout_width ="match_parent"
        android:layout_height="wrap_content"
        android:text         ="CALCULATE"
        android:id="@+id/button1" />

</LinearLayout>

正确的用法如下

在您的活动中:

Button calculateButton;
protected override void OnCreate(Bundle savedInstanceState)
  {
      base.OnCreate(savedInstanceState);
      // Create your application here
      SetContentView(Resource.Layout.activity_main);
      calculateButton = FindViewById<Button>(Resource.Id.calculateButton);
      calculateButton.Click += delegate
        {
            "insert calculations"
        };
  }

然后在相应的 axml(这里是activity_main(:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <Button
     android:id="@+id/calculateButton"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"   
     android:text="Calculate"
    />
</RelativeLayout>     

最新更新