DroidScript提示token意外结束错误,请求代码排查与功能修复
Fixing "unexpected end of token" Error in Your DroidScript App
Hey there! Let's get that annoying unexpected end of token error sorted out. The problem boils down to mismatched curly braces and incorrect function nesting—here's exactly what went wrong and the fixed code:
What Caused the Error?
Your enter(), OnBack(), and yesnores() functions were accidentally nested inside the lvw_OnTouch() function, and you missed a closing curly brace for lvw_OnTouch() before defining these helper functions. DroidScript's parser couldn't properly read the code structure, leading to the token error.
Corrected Code
function OnStart() { list = app.LoadText("List"); lvw = app.CreateListView(list, "Lego"); lvw.SetOnTouch(lvw_OnTouch); lvw.Show(); app.EnableBackKey("false"); // Moved inside OnStart() for better initialization practice } function lvw_OnTouch(item) { if(item == "Add Item"){ to = app.LoadText("List"); // Create dialog window dlgTxt = app.CreateDialog("Add Item"); // Create a layout for dialog layDlg = app.CreateLayout("linear", "vertical,fillxy,left"); layDlg.SetPadding(0.02, 0, 0.02, 0.02); dlgTxt.AddLayout(layDlg); lstDlg = app.CreateTextEdit(""); lstDlg.SetHint("*******Item ID here*******"); lstDlg.SetPadding(0.0, 0, 0.02, 0.02); lstDlg.SetTextColor("#dddddd"); layDlg.AddChild(lstDlg); lstDlg.SetOnEnter(enter); amount = app.CreateTextEdit(""); amount.SetHint("*******Item Amount*******"); amount.SetPadding(0.0, 0, 0.02, 0.02); amount.SetTextColor("#dddddd"); layDlg.AddChild(amount); amount.SetOnEnter(enter); // Show dialog dlgTxt.Show(); } else if(item == "Reset"){ var yesno = app.CreateYesNoDialog("Do you really want to reset?"); yesno.SetOnTouch(yesnores); yesno.Show(); } else if(item == "Exit App"){ app.Exit(); } else { lvw.Show(); } } // Added this missing closing brace for lvw_OnTouch() function enter() { dlgTxt.Hide(); lvw.Show(); app.SaveText("List", to + "," + lstDlg.GetText() + " " + amount.GetText()); } function OnBack() { app.Exit(); } function yesnores(result) { if(result == "Yes") { app.SaveText("List", "Add Item,Reset,Exit App"); // Refresh list view to show reset changes immediately list = app.LoadText("List"); lvw.SetList(list); } else { lvw.Show(); } }
Key Changes Made
- Added the missing closing curly brace
}for thelvw_OnTouch()function to fix the token error - Moved
app.EnableBackKey("false")insideOnStart()(standard practice for initialization code) - Added a line to refresh the list view after resetting (
lvw.SetList(list)) so changes show up right away - Cleaned up indentation to make the code easier to read and debug later
Verify Your Features
- Add Item: Enter text and press Enter—your new item will save to the list file, and the dialog will close
- Reset: Confirm the reset, and the list will revert to its default entries with an immediate update
- Exit App: Tap this option or the back button to close the app as expected
内容的提问来源于stack exchange,提问作者kahveciderin
相关产品推荐
相关产品推荐

