Search code examples
javascripttitanium-mobile

Titanium error in creating a form


I am new to titanium appcelerator and getting an error while creating a form with textfields and buttons..In this page there are 3 textfields and a button

Here is my code

var win1 = Titanium.UI.currentWindow;
var aview = Titanium.UI.createView({
borderRadius : 10,
backgroundColor:'Red',
width : Titanium.UI.FILL,
height : Titanium.UI.FILL,
layout : 'vertical'
});
win1.add(aview);
    height : 'auto',
width : Titanium.UI.FILL,
top : 10,
left : 100,
right : 40,
bottom: 40,
text:'Registration',
color:'White'
 });
aview.add(headings);

var usern = Ti.UI.createLabel({
height : 'auto',
width : Titanium.UI.FILL,
top : 10,
left : 40,
right : 40,
text:'Username',

color:'black'
});
aview.add(usern);

var user = Ti.UI.createTextField({
borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
color: '#336699',
text:'Username',
height : 'auto',
width : Titanium.UI.FILL,
top : 10,
left : 40,
right : 40
});
aview.add(user);

var passn = Ti.UI.createLabel({
text:'Password',
height : 'auto',
width : Titanium.UI.FILL,
top : 10,
left : 40,
right : 40,    color:'black'
});
aview.add(passn);
var pass = Ti.UI.createTextField({
height : 'auto',
width : Titanium.UI.FILL,
top : 10,
left : 40,
right : 40,
text:'Password',
color: '#336699',
keyboardType: Titanium.UI.KEYBOARD_PHONE_PAD,// This has varities of keyboard and I choose  PHONE_PAD as layout and it displays number pad when we click on the edit box of the date
returnKeyType: Ti.UI.RETURNKEY_DONE,
passwordMask:true,//This makes the letter display in ***** format
  borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED

});
aview.add(pass);

var fname = Ti.UI.createLabel({
height : 'auto',
width : Titanium.UI.FILL,
top : 10,
left : 40,
right : 40,
text:'Fullname',
color:'black'
});
aview.add(fname);

var fn = Ti.UI.createTextField({
borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
color: '#336699',
text:'Fullname',
height : 'auto',
width : Titanium.UI.FILL,
top : 10,
left : 40,
right : 40

});
aview.add(fn);

var buttons = Ti.UI.createButton({
title: 'Submit',
height : 'auto',
width : 'auto',
top : 10,
left : '40%',
bottom : 10,
padding : '20%'
});
buttons.addEventListener('click',function(e)
{
  Ti.API.info("You clicked the button");
var win2 = Ti.UI.createWindow({  
url:'page2.js'
});
win2.open();
});

aview.add(buttons);

and these are the errors which I get when running the application,please help

Errors:

[ERROR] :  TiExceptionHandler: (main) [235,235] ----- Titanium Javascript Runtime Error -----
[ERROR] :  TiExceptionHandler: (main) [0,235] - In app.js:1,69
[ERROR] :  TiExceptionHandler: (main) [0,235] - Message: Uncaught TypeError: Cannot call method     'add' of null
[ERROR] :  TiExceptionHandler: (main) [0,235] - Source: h:Titanium.UI.FILL,height:Titanium.UI.FILL,layout:"vertical"});win1.add(aview)
[ERROR] :  V8Exception: Exception occurred at app.js:1: Uncaught TypeError: Cannot call method 'add' of null

Solution

  • Check this out : I think this is your app.js, if yes it will work.

    var win1 = Titanium.UI.createWindow({backgroundColor: "#ffffff"});
    win1.open();
    var aview = Titanium.UI.createView({
        borderRadius : 10,
        backgroundColor : 'Red',
        width : Titanium.UI.FILL,
        height : Titanium.UI.FILL,
        layout : 'vertical'
    });
    win1.add(aview);
    
    var headings = Ti.UI.createLabel({
        height : 'auto',
        width : Titanium.UI.FILL,
        top : 10,
        left : 100,
        right : 40,
        bottom : 40,
        text : 'Registration',
        color : 'White'
    });
    aview.add(headings);
    
    var usern = Ti.UI.createLabel({
        height : 'auto',
        width : Titanium.UI.FILL,
        top : 10,
        left : 40,
        right : 40,
        text : 'Username',
    
        color : 'black'
    });
    aview.add(usern);
    
    var user = Ti.UI.createTextField({
        borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
        color : '#336699',
        text : 'Username',
        height : 'auto',
        width : Titanium.UI.FILL,
        top : 10,
        left : 40,
        right : 40
    });
    aview.add(user);
    
    var passn = Ti.UI.createLabel({
        text : 'Password',
        height : 'auto',
        width : Titanium.UI.FILL,
        top : 10,
        left : 40,
        right : 40,
        color : 'black'
    });
    aview.add(passn);
    var pass = Ti.UI.createTextField({
        height : 'auto',
        width : Titanium.UI.FILL,
        top : 10,
        left : 40,
        right : 40,
        text : 'Password',
        color : '#336699',
        keyboardType : Titanium.UI.KEYBOARD_PHONE_PAD, // This has varities of keyboard and I choose  PHONE_PAD as layout and it displays number pad when we click on the edit box of the date
        returnKeyType : Ti.UI.RETURNKEY_DONE,
        passwordMask : true, //This makes the letter display in ***** format
        borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED
    
    });
    aview.add(pass);
    
    var fname = Ti.UI.createLabel({
        height : 'auto',
        width : Titanium.UI.FILL,
        top : 10,
        left : 40,
        right : 40,
        text : 'Fullname',
        color : 'black'
    });
    aview.add(fname);
    
    var fn = Ti.UI.createTextField({
        borderStyle : Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
        color : '#336699',
        text : 'Fullname',
        height : 'auto',
        width : Titanium.UI.FILL,
        top : 10,
        left : 40,
        right : 40
    
    });
    aview.add(fn);
    
    var buttons = Ti.UI.createButton({
        title : 'Submit',
        height : 'auto',
        width : 'auto',
        top : 10,
        left : '40%',
        bottom : 10,
        padding : '20%'
    });
    buttons.addEventListener('click', function(e) {
        Ti.API.info("You clicked the button");
        var win2 = Ti.UI.createWindow({
            url : 'page2.js'
        });
        win2.open();
    });
    
    aview.add(buttons);