lua中的TextField输入为空时出错?

lua中的TextField输入为空时出错?,lua,coronasdk,Lua,Coronasdk,问题是,当txt.text为空时,我会收到错误消息 --input text local txt = native.newTextField(160,100,300,50) --button press to get answer local btn = display.newRect(160,300,120,40) --label text to show answer local label = display.newText("answer",160,200) txt.inputTy

问题是,当txt.text为空时,我会收到错误消息

--input text 
local txt = native.newTextField(160,100,300,50)
--button press to get answer 
local btn = display.newRect(160,300,120,40)
--label text to show answer
local label = display.newText("answer",160,200)

txt.inputType = "number"

function doit(e)
    -- Currency exchange (USD to any country)
    label.text = txt.text * 2
    -- when txt.text == empty I get error
end

btn:addEventListener("tap",doit)

我尝试使用
if-else
,但同样的问题。

您只需要添加适当的
if
语句。尝试:

function doit (e)
    if tonumber(txt.text) then
        label.text = tonumber(txt.text) * 2
    end
end
代码的其余部分保持不变