当我尝试运行模拟器时,calculateButton.Click + =委托发生异常,我试图制作一个小费计算器,因此当用户单击按钮时,应用程序便开始计算。
该异常表明 抛出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>
答案 0 :(得分:0)
正确用法如下
您的活动中:
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>