我如何使用一个选项卡来访问 Lua 文件?

我在试图访问其中一个拥有选项卡的文件时出现了错误。有人知道我做错了什么吗?Corona SDK告诉我,我的错误出现在实际创建选项卡时的代码部分,而不是按钮。谢谢

下面是我在 MainPage.lua 场景中创建选项卡的代码。

local tabBar = nil
function scene:create(event)
local group = scene.view
local tabButtons =
{
    {
        width = 20,
        height = 32,
        defaultFile = "assets/home.png",
        overFile = "assets/PressHome.png",
        label = "Home",
        font = tabLabelFont,
        size = tabLabelFontSize,
        onPress = function() composer.gotoScene("Home"); end,
    },
    {
        width = 20,
        height = 32,
        defaultFile = "assets/Explore.png",
        overFile = "assets/Explore.png",
        label = "Explore",
        font = tabLabelFont,
        size = tabLabelFontSize,
        onPress = function() composer.gotoScene("Explore"); end,
    },
    {
        width = 20,
        height = 32,
        defaultFile = "assets/Post.png",
        overFile = "assets/Post.png",
        label = "Post",
        font = tabLabelFont,
        size = tabLabelFontSize,
        onPress = function() composer.gotoScene("Post"); end,
    },
    {
        width = 20,
        height = 32,
        defaultFile = "assets/Notification.png",
        overFile = "assets/Notification.png",
        label = "Notification",
        font = tabLabelFont,
        size = tabLabelFontSize,
        onPress = function() composer.gotoScene("Notification"); end,
    },
    {
        width = 20,
        height = 32,
        defaultFile = "assets/Profile.png",
        overFile = "assets/Profile.png",
        label = "Profile",
        font = tabLabelFont,
        size = tabLabelFontSize,
        onPress = function() composer.gotoScene("Profile"); end,
    }
}

-- 创建一个选项卡并将其放置在屏幕底部
tabBar = widget.newTabBar
{
    top = display.contentHeight - 50,
    width = display.contentWidth,
    backgroundFile = "assets/tabbar.png",
    tabSelectedLeftFile = "assets/tabBar_tabSelectedLeft.png",
    tabSelectedMiddleFile = "assets/tabBar_tabSelectedMiddle.png",
    tabSelectedRightFile = "assets/tabBar_tabSelectedRight.png",
    tabSelectedFrameWidth = 20,
    tabSelectedFrameHeight = 52,
    buttons = tabButtons
}
group:insert(tabBar)
composer.gotoScene("Home")
end
scene:addEventListener("create", scene)
return scene

以下是从登录页面访问选项卡页的方法。

local function EnterPage(event)
    composer.gotoScene("MainPage")
end

这是错误的内容

栈追踪:
    ?:在函数中“?”
    ?:在函数<?: 703>中
    (tail call):?
    ?:在函数<?: 122>中
    (tail call):?
    MainPage.lua:127:在主要块中
    [C]:在函数“需要”中
    ?:在函数<?: 797>中
    (tail call):?
    Login.lua:78:在函数<Login.lua:70>中
    ?:在函数<?: 218>中

Main Page 的第 127 行是声明“tabBar = widget.newTabBar”的那行。 日志页面中的错误只是指向了gotoScene(MainPage)方法。

点赞
用户869951
用户869951

"File: ? Attempt to index a nil value" 很有帮助:它表明您正在进行一个形式为 a.b 的操作,例如 a.b = somethinga.b(),其中 a 是空值。根据它发生的行,这肯定是因为您忘记在文件顶部附近添加

widget = require 'widget'

如果此行已经存在(在 MainPage.lua 中),则检查是否已经覆盖了 widget

print('widget is', widget)
tabBar = widget.newTabBar
...

如果不是空值,则表明您在发布的信息(行号错误等)中犯了错误,或者可能尝试了许多事情并混淆了信息。为了确保,您可以尝试

widget = require 'widget'
tabBar = widget.newTabBar

如果这个有效,则说明您正在您的 MainPage.lua 中的某个地方覆盖 widget,但未显示在代码中。您还应该尝试

test = widget.newButton
{
    left = 100, top = 200,
    label = "Default",
    onEvent = function() print('hi') end
}

如果这个有效,但是 newTabBar 仍然不起作用,请尝试将您的选项替换为 corona newTabBar 网站上的示例选项。

2014-04-18 22:24:48