简体   繁体   中英

Uncaught TypeError: Cannot call method 'Method name' of undefined phonegap android

I am new to PhoneGap application development so I prepared a sample to test the platform. But unfortunately, when I ran the application, I received the below error:

Uncaught TypeError: Cannot call method 'clickOnAndroid' of undefined.

It was working yesterday but today suddenly stopped.

index.html

app.initialize();
$('document').ready(function() {    
    $('#current_date').click(function() {               
        window.AndroidShareFunction.clickOnAndroid();           
    })
}); 

Main activity:

final class myInterface {
    myInterface() {
    }
    public void clickOnAndroid() {
        Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
    }
}

Since you are running your JavaScript application within the PhoneGap platform, you will need to wait until PhoneGap has been completely initialized. In other words, although your document is ready because of $('document').ready(...) , some of the device's and/or PhoneGap's functionality may not be present including the initialization of window.AndroidShareFunction . To properly wait for PhoneGap to initialize, use the below code:

var callback = function () {
    window.AndroidShareFunction.clickOnAndroid(); 
};

$(document).ready(function () {
    document.addEventListener("deviceready", callback, false);
};

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM