繁体   English   中英

声明按钮单击时出错-Android C#Xamarin

[英]Error when declaring Button Click - Android C# Xamarin

我正在尝试为Click事件声明一个ImageButton ,但是给出了一个错误。 是什么会给这个错误?

(错误发生在btnSettingsBack.Click += btnSettingsBack_Click;

MainActivity代码(OnCreate):

    ImageButton btnSettings = FindViewById<ImageButton>(Resource.Id.btnSettings);
    ImageButton btnSettingsBack = FindViewById<ImageButton>(Resource.Id.btnSettingsBack);
    btnAdd = FindViewById<Button>(Resource.Id.btnAdd);
    lvNotes = FindViewById<ListView>(Resource.Id.lvNotes);
   dbHelper = new DbHelper(this);

    //Load Data
    LoadNoteList();

    btnAdd.Click += BtnAdd_Click;

    btnSettings.Click += BtnSettings_Click;

    btnSettingsBack.Click += BtnSettingsBack_Click;

}

错误:

错误图片

更新1: Settings.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"
    android:background="#fff"
    android:minWidth="25px"
    android:minHeight="25px">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:id="@+id/linearLayout1"
        android:padding="20px"
        android:background="#22A7F0">
        <ImageButton
            android:src="@drawable/menu_back"
            android:background="#22A7F0"
            android:layout_weight="0.6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="30px"
            android:id="@+id/btnSettingsBack"
            android:layout_gravity="center" />

更新2: OnCreate代码

protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            ActionBar.Hide();



            ImageButton btnSettings = FindViewById<ImageButton>(Resource.Id.btnSettings);
            ImageButton btnSettingsBack = FindViewById<ImageButton>(Resource.Id.btnSettingsBack);
            btnAdd = FindViewById<Button>(Resource.Id.btnAdd);
            lvNotes = FindViewById<ListView>(Resource.Id.lvNotes);
           dbHelper = new DbHelper(this);

            //Load Data
            LoadNoteList();

            btnAdd.Click += BtnAdd_Click;

            btnSettings.Click += BtnSettings_Click;

            btnSettingsBack.Click += BtnSettingsBack_Click;

        }

您正在将名为Main.axml的文件设置为您的活动的contentView

SetContentView(Resource.Layout.Main);

但是您在另一个名为Settings.axml的文件中定义了ImageButton

这就是为什么btnSettingsBack为null并因此出错的原因。

这些控件中的每一个都必须存在于您设置为页面的ContentView的Layout中。 如果不存在任何一个,则在尝试访问它时将出现NullReferenceException。

ImageButton btnSettings = FindViewById<ImageButton>(Resource.Id.btnSettings);
ImageButton btnSettingsBack = FindViewById<ImageButton>(Resource.Id.btnSettingsBack);
btnAdd = FindViewById<Button>(Resource.Id.btnAdd);

想想FindViewById是一种使用传递的ResourceId来查询提供给Activity的Layout的方法。 如果没有提供此ID,则此方法将不会投诉,但它将返回Null,因此我们的工作是确保您传递的每个ID都是您要设置的布局的一部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM