gweather_location_get_city_name:断言 'loc != NULL' 失败

gweather_location_get_city_name: assertion 'loc != NULL' failed

我正在尝试使用 GeoClue 和 GWeather 获取我的位置。

我想在空白处显示我所在城市的名称space。

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 () returns NULL 的情况(如果它找不到您的位置),并初始化 GWeather 位置以便它可以调用 find_nearest_city ().