我有一个带有取消和保存按钮的简单对话框片段。单击取消时,对话框将被取消。当我点击保存按钮时发生错误说。 “你调用的对象是空的”。我真的不明白我错在哪里。
这是我的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>
答案 0 :(得分:0)
您的EditText名为numOfdays(小'd')。 我不知道numOfDays(带有大写'D')的定义在哪里以及如何定义,为什么你没有得到编译器错误,但无论如何我认为它总是为null,因为你永远不会为它赋值。有时这些错误只需要看到另一只眼睛: - )