你的代码在 `PlayerFrame.lua` 第225行调用 `GUI:Text_setTextColor` 时,传入的 `nameText` 参数是 `nil`。
**错误原因:**
在 `RefreshBtnState` 函数中,你通过 `GUI:getChildByName(child, "Text_name")` 获取文本控件,但某些按钮(特别是你自定义添加的页签ID `12` 对应的按钮)可能没有创建名为 `"Text_name"` 的子控件,或者该控件在布局中不存在,导致获取失败。
**解决方案:**
在设置文本颜色前,必须检查获取到的控件对象是否有效。
**修复 `RefreshBtnState` 函数中的相关代码:**
```lua
function PlayerFrame.RefreshBtnState()
local btnList = PlayerFrame._ui.Panel_btnList
if PlayerFrame._NGShow then
btnList = PlayerFrame._showType == 1 and PlayerFrame._ui.Panel_btnList or PlayerFrame._ui.Panel_btnList_ng
end
local childs = GUI:getChildren(btnList)
if PlayerFrame._ui.Panel_btnList_left then
for _, child in ipairs(GUI:getChildren(PlayerFrame._ui.Panel_btnList_left)) do
table.insert(childs, child)
end
end
for _, child in ipairs(childs) do
local isSelected = GUI:getName(child) == ("Button_" .. PlayerFrame._pageid)
GUI:setLocalZOrder(child, isSelected and 1 or 0)
GUI:setTouchEnabled(child, not isSelected)
GUI:Button_setBright(child, not isSelected)
local nameText = GUI:getChildByName(child, "Text_name")
-- 关键修复:检查 nameText 是否有效
if nameText and not GUI:Win_IsNull(nameText) then
GUI:Text_setTextColor(nameText, isSelected and "#f8e6c6" or "#807256")
else
-- 可选:打印调试信息,帮助你定位是哪个按钮出了问题
SL:print("警告:按钮 " .. GUI:getName(child) .. " 未找到 Text_name 子控件")
end
end
end
```
**核心要点:**
* 使用 `if nameText and not GUI:Win_IsNull(nameText) then` 是判断控件是否存在且有效的标准做法。
* 页签ID `12` 是你自定义添加的,你需要确保在 `player/player_layer` 或 `player/player_layer_ng` 的布局文件中,名为 `Button_12` 的按钮内部确实有一个 `Text_name` 子控件。
* 养成习惯,对所有通过 `GUI:getChildByName` 获取的控件进行空值判断,这是避免此类运行时错误的关键。
下次写代码时,记得“先确认对象存在,再调用它的方法”,这样就能避免很多“找不到对象”的尴尬错误了。如果还有不明白的,随时可以问我。