У меня есть представление вывода, которому назначен идентификатор, и он присутствует в resource.designer.cs
Проблема в том, что я пытаюсь сослаться на представление в моем действии настроек, поэтому я могу использовать onDataEntered для получения значений, но моя ссылочная переменная всегда имеет значение NULL. Хотя использование точек останова говорит мне, что представление, на которое я ссылаюсь, не является нулевым.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Gms.Ads;
using Android.Preferences;
using Com.Goodiebag.Pinview;
using static Com.Goodiebag.Pinview.Pinview;
namespace JourneyAssistant
{
[Activity(Label = "Settings")]
public class SettingsActivity : Activity, IPinViewEventListener
{
//Variable declarations
AdView mAdView;
ISharedPreferences preferences;
ISharedPreferencesEditor editor;
Switch hideAddress;
Button saveBTN;
EditText streetName;
EditText area;
EditText town;
EditText county;
EditText postcode;
Pinview pinView;
string pinViewPin;
public string userPin;
bool hideAddressStatus;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_settings);
//Initialise Xamarin Essentials API
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
pinView = new Pinview(this);
streetName = FindViewById<EditText>(Resource.Id.streetName);
area = FindViewById<EditText>(Resource.Id.area);
town = FindViewById<EditText>(Resource.Id.town);
county = FindViewById<EditText>(Resource.Id.county);
postcode = FindViewById<EditText>(Resource.Id.postcode);
saveBTN = FindViewById<Button>(Resource.Id.saveBTN);
mAdView = FindViewById<AdView>(Resource.Id.adView);
hideAddress = FindViewById<Switch>(Resource.Id.hideAddress);
pinView = FindViewById<Pinview>(Resource.Id.pinEntry);
EditText[] editTexts = new EditText[] { streetName, area, town, county, postcode };
MobileAds.Initialize(this, "ca-app-pub-5407142743467927~1824688848");
AdRequest adRequest = new AdRequest.Builder().Build();
mAdView.LoadAd(adRequest);
preferences = PreferenceManager.GetDefaultSharedPreferences(this);
hideAddressStatus = preferences.GetBoolean("hideAddress", false);
userPin = preferences.GetString("userPin", "Pin not set");
if (hideAddressStatus == true)
{
pinView.SetPinViewEventListener(this); <--This gives a null exception**
hideAddress.Checked = true;
streetName.Text = preferences.GetString("streetName", "");
area.Text = preferences.GetString("area", "");
town.Text = preferences.GetString("town", "");
county.Text = preferences.GetString("county", "");
postcode.Text = preferences.GetString("postcode", "");
streetName.Visibility = ViewStates.Invisible;
area.Visibility = ViewStates.Invisible;
town.Visibility = ViewStates.Invisible;
county.Visibility = ViewStates.Invisible;
postcode.Visibility = ViewStates.Invisible;
}
else
{
pinView.SetPinViewEventListener(this);
hideAddress.Checked = false;
streetName.Text = preferences.GetString("streetName", "");
area.Text = preferences.GetString("area", "");
town.Text = preferences.GetString("town", "");
county.Text = preferences.GetString("county", "");
postcode.Text = preferences.GetString("postcode", "");
streetName.Visibility = ViewStates.Visible;
area.Visibility = ViewStates.Visible;
town.Visibility = ViewStates.Visible;
county.Visibility = ViewStates.Visible;
postcode.Visibility = ViewStates.Visible;
};
hideAddress.CheckedChange += (s, e) =>
{
if (e.IsChecked)
{
if (pinViewPin == "")
{
Toast.MakeText(this, "Please set a PIN",ToastLength.Short).Show();
}
hideAddressStatus = true;
streetName.Visibility = ViewStates.Invisible;
area.Visibility = ViewStates.Invisible;
town.Visibility = ViewStates.Invisible;
county.Visibility = ViewStates.Invisible;
postcode.Visibility = ViewStates.Invisible;
}
else
{
hideAddressStatus = false;
streetName.Visibility = ViewStates.Visible;
area.Visibility = ViewStates.Visible;
town.Visibility = ViewStates.Visible;
county.Visibility = ViewStates.Visible;
postcode.Visibility = ViewStates.Visible;
}
};
saveBTN.Click += delegate
{
foreach (EditText e in editTexts)
{
if (e.Text == "")
{
Toast.MakeText(this, "All address fields are required", ToastLength.Short).Show();
break;
}
}
saveSettings();
};
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
public void saveSettings()
{
editor = preferences.Edit();
editor.PutString("streetName", streetName.Text);
editor.PutString("area", area.Text);
editor.PutString("town", town.Text);
editor.PutString("county", county.Text);
editor.PutString("postcode", postcode.Text);
editor.PutBoolean("hideAddress", hideAddressStatus);
editor.Commit();
Toast.MakeText(this, "Settings saved", ToastLength.Short).Show();
}
public void OnDataEntered(Pinview pinView, bool fromUser)
{
Toast.MakeText(this, "" + pinView.Value, ToastLength.Short).Show();
}
}
}
Кто-нибудь знает, что происходит, пожалуйста?