Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Question about windows and views

  • Titanium 1.1.0
  • Android SDK 1.6
  • QVGA

Ok, I'm having some issues figuring out how to use windows/views in the mobile environment. When I look at examples from KitchenSink such as network.js, the code simply adds labels directly to the initial window. But when I try the same, things blow up.

Basically, is what I am wanting is to added labels/buttons/etc directly to the main window in the app. That way, I don't have to close extra windows when I kill the app.


That is the code I'm trying to use, which is based on network.js from KitchenSink. The error I'm seeing is:

[TRACE] E/AndroidRuntime( 1444): org.mozilla.javascript.EcmaError: TypeError: Cannot call method "add" of null

Considering there is only one place where I use "add", I have to assume I am doing something wrong with the "win" object.

Can anyone shed some light on this issue, or point me to a working example of how to get this to work? Thanks.


— asked March 23rd 2010 by Nicholas Davey
  • android
  • api
  • documentation
  • examples
  • view
  • window

1 Answer

  • Accepted Answer

    You can only use Ti.UI.currentWindow in a window which has been created from its own separate javascript file.

    You can't use it in app.js because there is no 'main window' in the app, and you haven't created any of your own windows.

    Try replacing this line:

    var win = Titanium.UI.currentWindow;

    With this:

    var win = Ti.UI.createWindow({

    And then at the end of your code, or once you've added the label to the window, open the window with win.open().

    If you want a nav bar at the top of your windows, you'll need to use a tab group, although you can hide the tabs after creating them if you don't actually intend to use them (example).

    You also referred to having to close extra windows when you kill the app, but you don't have to take any action at all when the app is closed - unless you want to build in some additional mechanism yourself to save the state of the app so that you can restore it the next time the app is opened.

    You might find the Snapost example to be a useful starting point for your first app.

    — answered March 23rd 2010 by James K
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.