setTimeout send an exception: "interface not implemented"

You must Login before you can answer or comment on any questions.

I'm trying to create a windows with a counter that should increase one every 2 seconds. Here the code:

MainWindow = function() {
 
    var win = Ti.UI.createWindow({
        backgroundColor : 'white'
    });
 
 
        var showCounterLabel = Titanium.UI.createLabel({
            color:'#999',
            text:'undef',
            font:{fontSize:20,fontFamily:'Helvetica Neue'},
            textAlign:'center',
            height : '50dp',
            width : '100dp',
        });
 
        var updateCounterLabel = function() {
            setTimeout ('updateCounterLabel()',2000);
                counter = Ti.App.Properties.getInt('counter',0);
            Ti.App.Properties.setInt('counter', counter + 1);
            showCounterLabel.text = Ti.App.Properties.getInt('counter',0);
        };
 
        var resetCounterButton = Ti.UI.createButton({
            title : 'resetCounterButton',
            height : '50dp',
            width : '100dp',
        top: '70dp',
        });
 
        resetCounterButton.addEventListener('click', function() {
            Ti.App.Properties.setInt('counter', 0);
            alert('reset done');
        });
 
 
        setTimeout ('updateCounterLabel();',2000);
 
        win.add(showCounterLabel);
        win.add(resetCounterButton);
 
        return win;
    };
 
module.exports = MainWindow;
Everything is compiled and running for the first 2 seconds, after that my App crashes with this message: >The application xxx (process yyy) has stopped unexpectedly. Please try again>

From the log I get:

E/TiApplication(  649): (KrollRuntimeThread) [1483,1483] Sending event: exception on thread: KrollRuntimeThread msg:java.lang.IncompatibleClassChangeError: interface not implemented; Titanium 2.1.1,2012/07/27 14:01,0fd84a2
E/TiApplication(  649): java.lang.IncompatibleClassChangeError: interface not implemented
E/TiApplication(  649):     at ti.modules.titanium.TitaniumModule$Timer.run(TitaniumModule.java:156)
E/TiApplication(  649):     at android.os.Handler.handleCallback(Handler.java:587)
E/TiApplication(  649):     at android.os.Handler.dispatchMessage(Handler.java:92)
E/TiApplication(  649):     at android.os.Looper.loop(Looper.java:130)
E/TiApplication(  649):     at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:104)
E/AndroidRuntime(  649): FATAL EXCEPTION: KrollRuntimeThread
E/AndroidRuntime(  649): java.lang.IncompatibleClassChangeError: interface not implemented
E/AndroidRuntime(  649):    at ti.modules.titanium.TitaniumModule$Timer.run(TitaniumModule.java:156)
E/AndroidRuntime(  649):    at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(  649):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(  649):    at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(  649):    at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:104)
W/ActivityManager(   62):   Force finishing activity by.wink/.WinkActivity

1 Answer

I think you want to use setInterval so that it runs repeatedly

http://www.w3schools.com/js/js_timing.asp

— answered 9 months ago by Aaron Saunders
answer permalink
3 Comments
  • remove the call to setTimeout in the callback

    change your existing setTimeout to setInterval

    var timer = setInterval (function(){
        updateCounterLabel();
    }, 2000);
    to clear it out later
    clearInterval(timer);

    — commented 9 months ago by Aaron Saunders

  • That wasn't the problem. I'm still having the same error with this code:

    var updateCounterLabel = function() {
                counter = Ti.App.Properties.getInt('counter',0);
                Ti.App.Properties.setInt('counter', counter + 1);
                showCounterLabel.text = Ti.App.Properties.getInt('counter',0);
            };
     
            setInterval ('updateCounterLabel()',2000);
    but it works fine with:
    var updateCounterLabel = function() {
                counter = Ti.App.Properties.getInt('counter',0);
                Ti.App.Properties.setInt('counter', counter + 1);
                showCounterLabel.text = Ti.App.Properties.getInt('counter',0);
            };
     
            setInterval (function () {updateCounterLabel();},2000);
    So the first argument of "setInterval" or "setTimeout" must be a callback NOT a string with a callback.

    Thanks, Domenico

    — commented 9 months ago by Domenico Pontari

  • I did make that edit also in the sample I provided, just forgot to call it out. With set interval you do not need to reset the timeout, that is why I recommended that change

    — commented 9 months ago by Aaron Saunders

Your Answer

Think you can help? Login to answer this question!