Xamarin中的按钮点击功能错误

本文关键字:功能 错误 按钮 Xamarin | 更新日期: 2023-09-27 18:25:33

我有一个带有取消和保存按钮的简单对话框片段。单击"取消"时,对话框将被取消。当我点击保存按钮时,出现了一个错误:。"对象引用未设置为对象的实例"。我真的不明白我哪里搞错了。

这是我的axml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">
    <TextView
        android:text="Set the number of days:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/setnumDays" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/numberOfDays"
        android:layout_below="@+id/setnumDays"
        android:textSize="20sp"
        android:layout_marginTop="5dp"
        android:layout_centerHorizontal="true" />
    <Button
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_below="@id/numberOfDays"
        android:text="Cancel"
        android:background="?android:attr/selectableItemBackground" />
    <Button
        android:id="@+id/save"
        android:background="?android:attr/selectableItemBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:layout_below="@id/numberOfDays"
        android:layout_toRightOf="@+id/cancel"
        android:layout_alignBaseline="@+id/cancel"
        android:text="Save" />
</RelativeLayout>

我的对话片段是:

using System;
using Android.App;
using Android.Content;
using Android.Support.V4.App;
using Android.Views;
using Android.Widget;
using MedAlert.Activities;
using Fragment = Android.Support.V4.App.Fragment;
//using TaskStackBuilder = Android.Support.V4.App.TaskStackBuilder;
using Android.Support.V7.App;
using Android.Support.Design.Widget;
using V7Toolbar = Android.Support.V7.Widget.Toolbar;
using Android.OS;
namespace MedAlert.Fragments
{
    public class DurationDialog : Android.Support.V4.App.DialogFragment
    {
        EditText numOfdays;
        public DurationDialog()
        {
            this.RetainInstance = true;
        }
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Android.OS.Bundle savedInstanceState)
        {
            var ignored = base.OnCreateView(inflater, container, savedInstanceState);
            var view = inflater.Inflate(Resource.Layout.dialog_duration, container, false);
            var cancel = view.FindViewById<Button> (Resource.Id.cancel);
            var save = view.FindViewById<Button> (Resource.Id.save);
            numOfdays = view.FindViewById<EditText> (Resource.Id.numberOfDays);
            cancel.Click += (object sender, EventArgs e) => {
                Dialog.Dismiss();
            };
            save.Click += Save_Click;
            return view;
        }
        void Save_Click (object sender, EventArgs e)
        {
            if (addMedFragment.mode == "ADD") {
                try{
                    addMedFragment.duration = Int32.Parse(numOfDays.Text);
                    Dialog.Dismiss ();
                }catch(Exception ex){
                    Toast.MakeText(Activity, ex.Message, ToastLength.Long).Show();
                    Dialog.Dismiss ();
                }
            }
            if (addMedFragment.mode == "EDIT"){
                try{
                    EDmedFragment.duration = Int32.Parse(numOfDays.Text);
                    Dialog.Dismiss ();
                }catch(Exception ex){
                    Toast.MakeText(Activity, ex.Message, ToastLength.Long).Show();
                    Dialog.Dismiss ();
                }
            }
        }
        public override void OnActivityCreated(Bundle savedInstanceState)
        {
            Dialog.Window.RequestFeature (WindowFeatures.NoTitle);
            base.OnActivityCreated (savedInstanceState);
        }
    }
}

请帮忙。我真的需要在周末之前解决这个问题。谢谢在我的应用程序输出选项卡上有这些:

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.NullReferenceException: Object reference not set to an instance of an object
[MonoDroid] at MedAlert.Fragments.DurationDialog.OnCreateView (Android.Views.LayoutInflater,Android.Views.ViewGroup,Android.OS.Bundle) [0x0003e] in c:'Users'xhemMiaow~'Documents'Projects'MedAlert'MedAlert'Fragments'DurationDialog.cs:34
[MonoDroid] at Android.Support.V4.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ (intptr,intptr,intptr,intptr,intptr) <IL 0x00026, 0x000fa>
[MonoDroid] at (wrapper dynamic-method) object.9e951dbb-09ac-4bc9-accc-8ff86fa9323b (intptr,intptr,intptr,intptr,intptr) <IL 0x00023, 0x00047>

Xamarin中的按钮点击功能错误

您的EditText命名为numOfdays(小写'd')。我不知道numOfDays(大写为"D")是在哪里以及如何定义的,为什么你不得到一个编译器错误,但无论如何,我认为它总是空的,因为你从来没有给它赋值。有时这些错误只需要另一只眼睛就能看到:-)