[LUA-print] ----------------------------------------
[LUA-print] LUA ERROR: target is nil value
[LUA-print] stack traceback:
[string "scripts/GUI/GUI.lua"]:101: in function 'CheckIsInvalidCObject'
[string "scripts/GUI/GUI.lua"]:4223:
根据浮生梦老师前端课程内容,这个错误是因为在调用 `GUI:CheckBox_addOnEvent(checkBox, function() ... end)` 时,`checkBox` 参数是 `nil` 值。
错误发生在你的代码第87行(`[string "qianduan/eqrefined.lua"]:87`),具体是这段逻辑:
```lua
-- 获取复选框状态
for m, n in ipairs(npc.checkBoxStatus) do
local after_attribute = GUI:getChildByName(npc.ui.ItemShow_1, "after_attribute"..m)
local checkBox = GUI:getChildByName(after_attribute, "CheckBox_"..m)
if n == 1 then
GUI:CheckBox_setSelected(checkBox, true)
else
GUI:CheckBox_setSelected(checkBox, false)
end
GUI:CheckBox_addOnEvent(checkBox, function() -- 这里checkBox可能是nil
local status = GUI:CheckBox_isSelected(checkBox)
npc.lockStatus[npc.selectBtn][m] = status and 1 or 0
end)
end
```
**问题分析:**
1. `GUI:getChildByName` 可能返回 `nil`,如果找不到对应的子节点
2. 你没有对 `checkBox` 进行空值判断就直接使用
3. 当 `after_attribute` 为 `nil` 时,`GUI:getChildByName(after_attribute, ...)` 肯定返回 `nil`
**修复方案:**
```lua
-- 获取复选框状态
for m, n in ipairs(npc.checkBoxStatus) do
local after_attribute = GUI:getChildByName(npc.ui.ItemShow_1, "after_attribute"..m)
if not GUI:Win_IsNull(after_attribute) then -- 先判断父节点是否存在
local checkBox = GUI:getChildByName(after_attribute, "CheckBox_"..m)
if not GUI:Win_IsNull(checkBox) then -- 再判断复选框是否存在
if n == 1 then
GUI:CheckBox_setSelected(checkBox, true)
else
GUI:CheckBox_setSelected(checkBox, false)
end
GUI:CheckBox_addOnEvent(checkBox, function()
local status = GUI:CheckBox_isSelected(checkBox)
npc.lockStatus[npc.selectBtn][m] = status and 1 or 0
end)
else
SL:print("警告:找不到复选框 CheckBox_"..m)
end
else
SL:print("警告:找不到属性容器 after_attribute"..m)
end
end
```
**额外建议:**
1. 在调用任何GUI函数前,都应该用 `GUI:Win_IsNull(widget)` 检查控件是否有效
2. 你的洗练按钮点击事件不应该放在循环内部,应该放在外面
3. 考虑使用更安全的控件获取方式,或者确保布局文件中的节点名称正确
下次写代码时要注意空值判断,这是Lua开发中常见的错误。不懂的地方可以问浮生梦尊师!