【问题标题】:Button Clicked function error in XamarinXamarin 中的按钮单击功能错误
【发布时间】:2015-12-04 05:43:02
【问题描述】:

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

这是我的 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>

【问题讨论】:

  • 它出现在哪一行?
  • addMedFragment.duration = Int32.Parse(numOfDays.Text);和 EDmedFragment.duration = Int32.Parse(numOfDays.Text);这些行来自我的 DialogFragment。
  • 我正在使用 Xamarin Studio @DavidPilkington
  • 但是如果我直接将一个数字传递给 addMedFragment.duration 和 EDMEdFragment.duration,就可以了。
  • 我想我已经找到了问题所在。它在我的 EditText 变量中。不知何故,当我尝试为 numOfDays.Text 赋值时,它给出了对象。 .废话。 .等等错误。但我仍然不知道如何解决它:(

标签: c# android object xamarin dialog


【解决方案1】:

您的 EditText 被命名为 numOfdays(小“d”)。 我不知道在哪里以及如何定义 numOfDays(大写“D”)以及为什么不出现编译器错误,但无论如何我认为它始终为空,因为您从未为其赋值。有时这些错误只是需要另一只眼睛才能看到:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    相关资源
    最近更新 更多