我正在开发基于Xamarin框架的C#跨平台移动应用程序。以下代码使用setExactAndAllowWhileIdle
插件,该插件定义adapter
类型。此代码编译,但运行时,我得到以下NullReference错误消息,即使在程序的顶部定义了public partial class HomePage : ContentPage
{
// Class Definitions
ObservableCollection<IDevice> devices;
IAdapter adapter;
public HomePage(IAdapter adapter)
{
InitializeComponent();
this.adapter = adapter;
this.devices = new ObservableCollection<IDevice>();
Debug.WriteLine ("Before Button Clicked");
NewDeviceButton.Clicked += async (sender, e) => {
adapter.StartScanningForDevices (0x133D.UuidFromPartial ()); //Scan for a specific device
await Task.Delay (2000); // wait 2 seconds for the scan to complete
adapter.StopScanningForDevices ();
};
adapter.DeviceDiscovered += (object sender, DeviceDiscoveredEventArgs e) => {
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => {
devices.Add (e.Device);
});
};
}
}
。我错过了什么?
adapter.DeviceDiscovered
System.NullReferenceException:未将对象引用设置为对象的实例 在/Projects/my_App/My_App/Views/HomePage.xaml.cs:72的My_App.HomePage..ctor(IAdapter适配器)[0x00081]中
注意:第72行是app.cs
行。
这是HomePage
我的public class App : Application
{
static IAdapter Adapter;
public App ()
{
MainPage = new HomePage (Adapter);
}
public static void SetAdapter (IAdapter adapter) {
Adapter = adapter;
}
}
来自:
<ul id="featuresOptions" class="options" style="display: block;">
<li><label><input type="checkbox" name="Apartment" value="6"> Apartment</label></li>
<li><label><input type="checkbox" name="House" value="7"> House</label></li>
<li><label><input type="checkbox" name="Lot" value="8"> Lot</label></li>
<li><button class="button" id="featuresButton">Filter</button></li>
</ul>
$(document).on('click', "#featuresButton", function() {
var formData = [];
$("#featuresOptions li input").each(function(e) {
if($(this).is(":checked")) {
formData.push($(this).val());
}
});
});