Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] Crash with QAndroidJniObject::callStaticObjectMethod



  • Hi,

    I use QAndroidJniObject::callStaticObjectMethod to get a list of contacts which is an ArrayList<String>.

    My java code is :
    @
    public class contact extends org.qtproject.qt5.android.bindings.QtActivity {
    private static contact m_instance;
    private ArrayList<String> contactNameList = new ArrayList<String>();

    public contact(){
        m_instance = this;
    }
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    

    ...
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }
    
    public static ArrayList<String> getContacts(){
        Log.d(QtApplication.QtTAG, "Call getContacts");
        return m_instance.contactNameList;
    }
    

    }
    @

    my c++ code :
    @
    void ApplicationInfo::queryContacts()
    {
    qDebug("queryContacts");
    QAndroidJniObject contactNameVector = QAndroidJniObject::callStaticObjectMethod("org/qtproject/example/RDVU/contact",
    "getContacts",
    "()Ljava/util/ArrayList;");
    // Test if result is valid
    if( contactNameVector.isValid() ){
    jint size = contactNameVector.callMethod<jint>("size");
    for (int i = 0; i < size; ++i) {
    // Getting Contact name
    QAndroidJniObject jContactName = contactNameVector.callObjectMethod("get", "(I)Ljava/lang/Object;", i);
    QString contactName = jContactName.toString();
    qDebug(contactName.toStdString().c_str());
    }
    }
    }
    @

    I have this error :
    D/Qt (23861): ../RDVU/core/src/applicationinfo.cpp:85 (void RDVU::ApplicationInfo::queryContacts()): queryContacts
    D/Qt (23861): Call getContacts
    E/dalvikvm(23861): JNI ERROR (app bug): accessed stale local reference 0x15 (index 5 in a table of size 0)
    E/dalvikvm(23861): VM aborting

    I don't understand why I have this error. I read the Qt notification code example to search if I have missed something (like androidextras).

    I work with Android 4.4 and Qt 5.2.1.

    Thank you for your help.



  • The method getContacts() return the instance of class "contact". But I do not see any code that creates this instance. Also, the variable m_instance is not initialized, so the function returns a random value.



  • I have found !

    I forget add the contact activity in the AndroidManifest.xml :(


Log in to reply