本文介绍了Xamarin 中的按钮单击功能错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有取消和保存按钮的简单对话框片段.单击取消时,对话框被关闭.当我点击保存按钮时,会出现错误提示.你调用的对象是空的".我真的不明白我哪里错了.

I have a simple dialog fragment with a cancel and save button. When cancel is clicked dialog is dismissed. And when I clicked save button an error occur saying. "object reference not set to an instance of an object". I don't really understand where I got it wrong.

这是我的 axml 文件:

Here is my axml file:

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

请帮忙.我真的需要在本周结束前解决这个问题.谢谢!在我的应用程序输出选项卡上有这些:

Please help. I really need to solve this one before the week ends. Thanks!Having these on my application output tab:

[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>

推荐答案

您的 EditText 被命名为 numOfdays(小 'd').我不知道 numOfDays(带有大写的D")在何处以及如何定义,以及为什么您不会收到编译器错误,但无论如何我认为它始终为空,因为您从未为其分配值.有时这些错误只需要另一只眼睛就能看到:-)

Your EditText is named numOfdays (small 'd').I don't know where and how numOfDays (with capital 'D') is defined and why don't you get a compiler error, but anyway I think it is always null, since you never assign a value to it. Sometimes these errors just need another eyes to be seen :-)

这篇关于Xamarin 中的按钮单击功能错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:32