【问题标题】:Global variables have nil value, attempt to call global (a nil value) in Corona全局变量有 nil 值,尝试在 Corona 中调用 global(一个 nil 值)
【发布时间】:2015-05-09 01:32:21
【问题描述】:

我尝试通过函数中用户的文本框从用户那里获取数据(在全局变量中),并使用这些变量插入到 sqlite 表中。

但是,当我尝试这样做时,在单击提交按钮后,我在 INSERT INTO 行中收到类似“尝试调用全局 locationGlobal(一个 nil 值)”的错误。

注意:我使用 Bluestacks android 模拟器在文本框中输入值,因为在 windows Corona SDK 中无法输入文本。 这是我的代码:

local widget = require("widget")
require "sqlite3"   
local path = system.pathForFile("testUser.db", system.DocumentsDirectory)
db = sqlite3.open( path )  

--local location,area,arrivalTime,departTime,eventAttended 

local function onSystemEvent( event )
        if( event.type == "applicationExit" ) then              
            db:close()
        end
end

--local tablesetup = [[CREATE TABLE IF NOT EXISTS visitPlace (id INTEGER PRIMARY KEY autoincrement, location, area, arrivalTime, departTime, eventAttended);]]
local tablesetup = [[CREATE TABLE place (id INTEGER PRIMARY KEY autoincrement, location);]]
print(tablesetup)
db:exec( tablesetup )

_W = display.viewableContentWidth
_H = display.viewableContentHeight

local background = display.newRect(0,0,_W,_H)
background:setFillColor(0,0,0)    


local function textListenerLocation( event )

    if ( event.phase == "began" ) then
        -- user begins editing defaultField
        event.target.text = ''        
        print(location)            
        print( event.text )

    elseif ( event.phase == "ended" ) then
        -- do something with defaultField text          
         locationGlobal = tostring( event.target.text)             

    elseif ( event.phase == "editing" ) then
        print( event.newCharacters )
        print( event.oldText )
        print( event.startPosition )
        print( event.text )


    elseif ( event.phase == "submitted" ) then
        locationGlobal =tostring( event.target.text)            
        --local label = display.newText( location, 180, 30, native.systemFontBold, 20 )
       -- label:setFillColor( 190/255, 190/255, 1 )           
    end
end

local function SubmitEvent( event )
    --local label = display.newText( location, 180, 30, native.systemFontBold, 20 )
   --label:setFillColor( 190/255, 190/255, 1 )


    local insertionTable = [[INSERT INTO visitPlace VALUES(NULL,']]..locationGlobal..[[) ]]
    db:exec(insertionTable)

    for row in db:nrows("SELECT * FROM visitPlace") do
        local text = row.location
        local t = display.newText(text, 450, 120*row.id, native.systemFont, 40)
        t:setFillColor(1,0,1)

    end 
   local label1 = display.newText( "Submitted", 180, 30, native.systemFontBold, 20 )
   label1:setFillColor( 190/255, 190/255, 1 )
end

function background:tap( event )
    native.setKeyboardFocus(nil)
end



local locationTxtbox = native.newTextField(180,140,280,100)
locationTxtbox.size = 34
locationTxtbox:addEventListener("textListenerLocation",locationTxtbox)


local submitButton =  widget.newButton
{
    label = "Submit",
    onEvent = SubmitEvent,
    shape = "roundedRect",
    width = 100,
    height = 30,
    cornerRadius = 2    
}

submitButton.x = display.contentCenterX + (display.contentCenterX/2)
submitButton.Y = display.contentCenterY + (display.contentCenterY/2)

background:addEventListener("tap",background)
Runtime:addEventListener( "system", onSystemEvent )

--defaultField = native.newTextField( 150, 150, 180, 30 )

--defaultField:addEventListener( "userInput", textListener )

【问题讨论】:

    标签: lua coronasdk global null


    【解决方案1】:

    目前您的 textlistenerlocation 函数没有被调用。所以当你点击提交按钮时,locationGlobal 还没有被定义。

    以下内容看起来很可疑:

    locationTxtbox:addEventListener("textListenerLocation",locationTxtbox)
    

    addEventListener 应该接受一个事件字符串,然后是一个在事件发生时调用的函数。 textListenerLocation 是你的事件处理函数,它不是事件字符串。

    locationTxtbox:addEventListener("userInput", textListenerLocation)
    

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 2019-06-13
      • 2014-05-06
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 2014-11-05
      • 2019-03-01
      相关资源
      最近更新 更多