全局变量的值为零,试图在Corona中调用global(零值)

时间:2015-03-07 14:18:27

标签: lua corona global null

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

但是,当我尝试这样做时,我得到一个错误,例如"尝试调用全局locationGlobal(一个零值)"在单击提交按钮后,在INSERT INTO行中。

注意:我通过使用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 )

1 个答案:

答案 0 :(得分:0)

目前您的textlistenerlocation函数未被调用。因此,当您点击提交按钮时,尚未定义locationGlobal。

以下内容看起来很可疑:

locationTxtbox:addEventListener("textListenerLocation",locationTxtbox)

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

locationTxtbox:addEventListener("userInput", textListenerLocation)