如何解决gweather_location_get_city_name: 断言 'loc != NULL' 失败
我正在尝试使用 GeoClue 和 GWeather 获取我的位置。
public class Epoch.LabelsGrid : Gtk.Grid {
public Gtk.Label face1_label;
public Gtk.Label face2_label;
public Gtk.Label face3_label;
public Gtk.Label face4_label;
public GClue.Location? geo_location {get; private set; default = null;}
public GWeather.Location location;
private GClue.Simple simple;
construct {
face1_label = new Gtk.Label ("");
face1_label.label = dgettext ("libgweather-locations",location.get_city_name ());
face1_label.halign = Gtk.Align.CENTER;
face1_label.hexpand = true;
face1_label.margin_top = 6;
face1_label.set_ellipsize (END);
face1_label.set_max_width_chars (12);
face2_label = new Gtk.Label ("Paris");
face2_label.set_markup ("<span font_desc='Inter 14'><b>Paris</b></span>");
face2_label.halign = Gtk.Align.CENTER;
face2_label.hexpand = true;
face2_label.margin_top = 6;
face2_label.set_ellipsize (END);
face2_label.set_max_width_chars (12);
face3_label = new Gtk.Label ("London");
face3_label.set_markup ("<span font_desc='Inter 14'><b>London</b></span>");
face3_label.halign = Gtk.Align.CENTER;
face3_label.hexpand = true;
face3_label.margin_top = 6;
face3_label.set_ellipsize (END);
face3_label.set_max_width_chars (12);
face4_label = new Gtk.Label ("New York");
face4_label.set_markup ("<span font_desc='Inter 14'><b>New York</b></span>");
face4_label.halign = Gtk.Align.CENTER;
face4_label.hexpand = true;
face4_label.margin_top = 6;
face4_label.set_ellipsize (END);
face4_label.set_max_width_chars (12);
}
public async void seek () {
try {
simple = yield new GClue.Simple ("com.gihhub.Suzie97.epoch",GClue.AccuracyLevel.CITY,null);
} catch (Error e) {
warning ("Failed to connect to GeoClue2 service: %s",e.message);
return;
}
simple.notify["location"].connect (() => {
on_location_updated.begin ();
});
on_location_updated.begin ();
}
public async void on_location_updated () {
geo_location = simple.get_location ();
location = location.find_nearest_city (geo_location.latitude,geo_location.longitude);
}
}
这是代码。
代码可以编译,但在执行程序时,(com.github.Suzie97.epoch:30386): GWeather-CRITICAL **: 13:21:03.758: gweather_location_get_city_name: assertion 'loc != NULL' Failed
显示此警告。
我在 Daniel Fore 的名为 Nimbus (Link to nimbus repo)
的应用程序中发现了此代码片段 public void on_location_updated (double latitude,double longitude) {
location = GWeather.Location.get_world ();
location = location.find_nearest_city (latitude,longitude);
if (location != null) {
weather_info.location = location;
weather_info.update ();
stack.visible_child_name = "weather";
}
}
我需要做类似的事情吗?
解决方法
根据错误消息,gweather_location_get_city_name ()
发出错误,因为位置为 NULL(loc != NULL
为假)。看看你在哪里调用 get_city_name ()
。它位于构造函数的最顶部,在 location
设置为任何内容之前。您需要等到有位置后再尝试使用。
那么你应该使用类似 Nimbus 代码的东西。它处理 simple.get_location ()
返回 NULL 的情况(如果它找不到您的位置),并初始化 GWeather 位置以便它可以调用 find_nearest_city ()
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。