gtk - gtk_container_add:断言“GTK_IS_WIDGET(小部件)”失败
问题描述
public class Epoch.MainWindow : Gtk.ApplicationWindow {
private Gtk.Grid grid;
private Epoch.LabelsGrid labels;
private Epoch.PreferencesView preferences_view;
private Epoch.MainView main_view;
public MainWindow (Application app) {
Object (
application: app,
icon_name: "com.github.Suzie97.epoch",
resizable: false,
title: _("Epoch"),
width_request: 500
);
}
construct {
get_style_context ().add_class ("rounded");
set_keep_below (true);
stick ();
var preferences_button = new Gtk.Button.from_icon_name ("open-menu-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
preferences_button.valign = Gtk.Align.CENTER;
var preferences_stack = new Gtk.Stack ();
preferences_stack.add (preferences_view);
preferences_stack.add (main_view);
preferences_stack.transition_type = Gtk.StackTransitionType.SLIDE_LEFT;
var headerbar = new Gtk.HeaderBar ();
headerbar.show_close_button = true;
var headerbar_style_context = headerbar.get_style_context ();
headerbar_style_context.add_class ("default-decoration");
headerbar_style_context.add_class (Gtk.STYLE_CLASS_FLAT);
headerbar.pack_end (preferences_button);
set_titlebar (headerbar);
var main_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
main_box.pack_start (preferences_stack, true, true);
add (main_box);
show_all ();
preferences_view = new Epoch.PreferencesView ();
preferences_button.activate.connect (() => {
preferences_stack.visible_child = preferences_view;
});
// public override bool configure_event (Gdk.EventConfigure event) {
// int root_x, root_y;
// get_position (out root_x, out root_y);
// Epoch.settings.set_int ("window-x", root_x);
// Epoch.settings.set_int ("window-y", root_y);
// return base.configure_event (event);
}
}
只有当您看到时才会显示标题栏。
为什么会这样?我该如何解决?
解决方案
preferences_stack.add (preferences_view);
preferences_stack.add (main_view);
你还没有初始化preferences_view
,你也从来没有初始化main_view
. 这就是您的第二个和第三个错误的来源:gtk_container_add()
抱怨您尝试添加的小部件实际上不是小部件,而是null
因为您尚未初始化该变量。
推荐阅读
- office365 - 云端硬盘搜索在 Office 365 组中不起作用
- java - 扩展接口以更改合同,但不是 API
- vb.net - 如何在gridview windows窗体中更改单元格边框厚度的宽度?
- postgresql - 无法从我的 Linux 用户连接 PostgreSQL 数据库
- c# - 如何将 xaml 属性绑定到来自扩展的 DependencyProperty
- python-3.x - `python3`定义一个函数当`list[True, True, True]`时,当列表中存在`False`时返回`False`
- html - 如何使用填充垂直/水平居中 CSS 背景图像
- dax - 仅当上一季度访问过相同的客户 ID 时,如何明确计算在给定季度访问商店的客户 ID
- python - 使用数据库值构造动态 HTML 内容
- dart - 在调用listen方法之后,流的listen方法如何显示通过sink添加的数据?