全局变量具有 nil 值,尝试在 Corona 中调用 global(一个 nil 值)
Global variables have nil value, attempt to call global (a nil value) in Corona
我试图通过函数中用户的文本框从用户那里获取数据(在全局变量中),并使用这些变量插入 sqlite table。
但是,当我尝试这样做时,在我单击提交按钮后,在 INSERT INTO 行中出现类似 "attempt to call global locationGlobal (a nil value)" 的错误。
注意:我使用 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 )
当前未调用您的 textlistenerlocation 函数。因此,当您点击提交按钮时,locationGlobal 尚未定义。
以下看起来可疑:
locationTxtbox:addEventListener("textListenerLocation",locationTxtbox)
addEventListener 应该接受一个事件字符串,然后是一个在事件发生时调用的函数。 textListenerLocation 是你的事件处理函数,它不是事件字符串。
locationTxtbox:addEventListener("userInput", textListenerLocation)
我试图通过函数中用户的文本框从用户那里获取数据(在全局变量中),并使用这些变量插入 sqlite table。
但是,当我尝试这样做时,在我单击提交按钮后,在 INSERT INTO 行中出现类似 "attempt to call global locationGlobal (a nil value)" 的错误。
注意:我使用 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 )
当前未调用您的 textlistenerlocation 函数。因此,当您点击提交按钮时,locationGlobal 尚未定义。
以下看起来可疑:
locationTxtbox:addEventListener("textListenerLocation",locationTxtbox)
addEventListener 应该接受一个事件字符串,然后是一个在事件发生时调用的函数。 textListenerLocation 是你的事件处理函数,它不是事件字符串。
locationTxtbox:addEventListener("userInput", textListenerLocation)