Search code examples
androidandroid-actionbartitaniumtitanium-mobile

Why action bar in titanium crash my application?


am developing android app in titanium appcelerator. Now my problem is that i have to add action bar in my app and put two buttons in left and right side of it

but i cant successfully implement the action bar

i have done following to make show action bar but its just crashes ma application,

    var win = Ti.UI.createWindow({
        title: _args.title,
        backgroundColor:'black',
        navBarHidden: false,
        containingTab: _args.containingTab,
        //tabGroup: _args.tabGroup,
        barImage:rootPath+'/Components/top_bg.jpg'
    });


var actionBar;
win.addEventListener("open", function() {
    if (Ti.Platform.osname === "android") {
        if (! win.activity) {
            Ti.API.error("Can't access action bar on a lightweight window.");
            alert("NOT ACTIVITY");
        } else {
            actionBar = win.activity.actionBar;
            if (actionBar) {
                alert("ACTIVITY");
                actionBar.backgroundImage = "/images/bg_top.png";
                actionBar.title = "New Title";
                actionBar.onHomeIconItemSelected = function() {
                    Ti.API.info("Home icon clicked!");
                };
            }
        }
    }
});

can anyone please guide me where am doing mistake? or is there anything else that i have to follow.


Solution

  • try wrapping it in onCreateOptionsMenu

       win.activity.onCreateOptionsMenu = function(e) {
            actionBar = win.activity.actionBar;
            if (actionBar) {
                alert("ACTIVITY");
                actionBar.backgroundImage = "/images/bg_top.png";
                actionBar.title = "New Title";
                actionBar.onHomeIconItemSelected = function() {
                    Ti.API.info("Home icon clicked!");
                };
            } else {
                alert('missing action bar');
            }
         });