【问题标题】:UWP - Pedometer exceptionUWP - 计步器异常
【发布时间】:2017-04-05 14:00:17
【问题描述】:

我在 XAML TextBlock 中有名称为 StpTextBlock 和名称为 btnPedometra 的按钮。

也在代码中:

它正在返回这个异常:

【问题讨论】:

  • 尝试将您的代码显示为文本,以便其他人看到它的样子。出于同样的原因复制下面的异常文本,并且不要忘记标记引发异常的位置以及它是什么类型的异常。
  • 添加 NullReferenceException 的完整堆栈跟踪 - 在 TEXT 中
  • 正如@rudolf_franek 所说,请在文本而不是图片中分享代码和错误。仅使用屏幕截图,很难重现和识别您的问题。 :(

标签: c# xaml uwp-xaml


【解决方案1】:

局部变量

Pedometer readings

await Pedometer.GetdefaultAsync()赋值后保持为空

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

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

【讨论】:

  • 你能给我发链接吗?
  • 当前答案?因为我找不到它
  • 确实在上面的回复中——如果你觉得你的问题更广泛,你必须更好地描述它。
【解决方案2】:

正如@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

【讨论】:

    猜你喜欢
    • 2016-09-17
    • 1970-01-01
    • 2017-05-26
    • 2017-05-19
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    相关资源
    最近更新 更多