2017-04-05 64 views
0

我在XAML TextBlock中使用名称StpTextBlock和名称为btnPedometra的按钮。 UWP - 计步器异常

也在这个代码: enter image description here

和它返回此异常:enter image description here

+1

尝试将您的代码显示为文本,以便其他人看到它的样子。为了同样的原因,复制下面的异常文本,不要忘记标记抛出异常的地方以及它是什么类型的异常。 –

+0

添加你的NullReferenceException的完整堆栈跟踪 - 在文本 –

+0

正如@rudolf_franek所说,请分享代码和错误在**文本**不**照片**。只需截图,很难重现和识别您的问题。 :( – Scavenger

回答

0

局部变量

Pedometer readings 

仍然分配后,空从await Pedometer.GetdefaultAsync()

readings.Interval = 120;抛出异常,因为readings为空

您需要找出为什么Pedometer.GetdefaultAsync()返回null。

+0

你能给我发送链接吗? – theoooood

+0

为当前答案?因为我找不到它 – theoooood

+0

它确实在上面的回答 - 如果你觉得你的问题更广泛,你必须更好地描述它。 –

0

正如@ rudolf_franek的回答说,你得到了NullReferenceException因为 由Pedometer.GetdefaultAsync()返回readingsnull

Pedometer.GetdefaultAsync()返回代表默认传感器的Pedometer对象。如果没有计步器传感器,返回值将为空。因此,在使用Pedometer时,请确保您的设备具有计步器传感器。在代码中,通过确定Pedometer.GetdefaultAsync()的返回值是否为null来检查此问题。

var readings= await Pedometer.GetDefaultAsync(); 
if (null == readings) 
{ 
    MessageDialog showDialog = new MessageDialog("No pedometer available"); 
    await showDialog.ShowAsync(); 
} 
else 
{ 
    readings.ReportInterval = readings.MinimumReportInterval; 
    readings.ReadingChanged += Readings_ReadingChanged; 
} 

欲了解更多信息,请参阅GitHub上的官方Pedometer sample