Simple button
Okay, all I am trying to do is create an initial window with 2 buttons on, these buttons will then link out to 2 seperate windows. I have written the code, but when I run it, I get:
Sorry!
The application (…….) has stopped unexpectedly. Please try again.
Not sure what the problem is. Is there a nice document somewhere stating good practice, and how to debug your own code in windows with these sorts of issues?
The whole file is:
// Insert initial window
var theWindow = Titanium.UI.createWindow();
theWindow.open({fullscreen:true});
// Insert the MAP button and listener
var mapButton = Titanium.UI.createButton({
title: 'Map',
image:'../images/chat.png',
width:200,
height:40,
top:60
});
mapButton.addEventListener('click',function(e)
{
Titanium.API.info("Map");
});
// Insert the HELP OUT button and listener
var helpOutButton = Titanium.UI.createButton({
title: 'Help Out',
image:'../images/chat.png',
width:200,
height:40,
top:60
});
helpOutButton.addEventListener('click',function(e)
{
Titanium.API.info("Help Out");
});
// Add buttons to window
theWindow.add(mapButton);
theWindow.add(helpOutButton);
Thankyou for any pointers, or documentation to help in future debugging/learning,
Dan
4 Answers
-
Hi,
I cannot debug your code right now, as I am not at my work pc, but have you tried to look into the trace? Just set the log level in Titanium to trace, left of the launch button.
Also the KitchenSink examples are of great help.
Good luck -
Hi Dan,
Try putting the 'theWindow.open command at the end (setting everything else up first).
Otherwise I think you will need to use Titanium.UI.currentWindow.theWindow to attach buttons after. Although, because the 'window.open' is first, the window will display first then add the buttons after (which may not look so great).
cheers,
Chris. -
Thanks Chris, sadly putting the open() command at the end did nothing, the error still appeared. So I then tried using currentWindow (code below), the window displayed now with no error, but was just a blank screen, no buttons.
Could it be something wrong with the buttons getting added, or a faulty API installed at my end?
// Add buttons to window Titanium.UI.currentWindow.theWindow.add(mapButton); Titanium.UI.currentWindow.theWindow.add(helpOutButton);
-
Got it, not exactly sure what I did, looks like a few things, and a complete restart of titanium, but here is the code which worked:
// Insert initial window var theWindow = Titanium.UI.createWindow(); // Insert the MAP button and listener var mapButton = Titanium.UI.createButton({ title: 'Map', width:200, height:40, top:60 }); mapButton.addEventListener('click',function(e) { Titanium.API.info("Map"); }); // Insert the HELP OUT button and listener var helpOutButton = Titanium.UI.createButton({ title: 'Help Out', width:200, height:40, top:120 }); helpOutButton.addEventListener('click',function(e) { Titanium.API.info("Help Out"); }); theWindow.add(mapButton); theWindow.add(helpOutButton); theWindow.open({fullscreen:true});
Basic process of this is:
- Declare window
- Declare each button
- Add buttons to window
- Open window