【问题标题】:gweather_location_get_city_name: assertion 'loc != NULL' failedgweather_location_get_city_name:断言'loc!= NULL'失败
【发布时间】:2021-04-06 08:02:36
【问题描述】:

我正在尝试使用 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) 的应用程序中发现了这段代码 sn-p

 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";
        }
    }

我需要做类似的事情吗?

【问题讨论】:

    标签: location gtk vala


    【解决方案1】:

    根据错误消息,gweather_location_get_city_name () 发出错误,因为位置为 NULL(loc != NULL 为假)。看看你在哪里打电话get_city_name ()。它位于构造函数的最顶端,在 location 设置为任何值之前。您需要等到有位置后再尝试使用它。

    那么你应该使用类似 Nimbus 代码的东西。它处理simple.get_location () 返回 NULL 的情况(如果它找不到您的位置),并且还初始化 GWeather 位置以便它可以调用find_nearest_city ()

    【讨论】:

    • 我一开始使用seek.begin ()初始化seek方法,然后在on_location_updated方法中使用这段代码添加城市标签` if (location != null) { face1_label.label = dgettext ("libgweather-locations", location.get_city_name ()); } `
    • 但是会显示这个警告:** (com.github.Suzie97.epoch:28509): WARNING **: 21:00:48.419: Labels.vala:71: Failed to connect to GeoClue2 service: GDBus.Error:org.freedesktop.DBus.Error.AccessDenied: Agent rejected 'com.gihhub.Suzie97.epoch' for user '1000'. Please ensure that 'com.gihhub.Suzie97.epoch' has installed a valid com.gihhub.Suzie97.epoch.desktop file.
    • 但是我已经为应用安装了一个 .desktop 文件。
    • com.gihhub.Suzie97.epoch
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    相关资源
    最近更新 更多