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

NoSuchMethodError with JNI during reading Shared Preferences in Android



  • Hi, I want to use the Shared Preferences in Android to save persistently the User-ID, also if the User deinstalls the app. For this I'm using JNI/Qt5.12 but during reading the ID I get an error.

    https://developer.android.com/training/data-storage/shared-preferences.html

    Reading:

    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    int defaultValue = getResources().getInteger(R.integer.saved_high_score_default_key);
    int highScore = sharedPref.getInt(getString(R.string.saved_high_score_key), defaultValue);
    

    JNI:

    jint mp = QAndroidJniObject::getStaticField<jint>("android/content/Context", "MODE_PRIVATE");
    QAndroidJniObject name = QAndroidJniObject::fromString("test");
    QAndroidJniObject activity = QtAndroid::androidActivity();
    QAndroidJniObject sharedPref = activity.callObjectMethod("getPreferences", "(I)Landroid/content/SharedPreferences;", mp);
    jint defId = 2;
    

    jint id = sharedPref.callMethod<jint>("getInt","(Ljava/lang/String;I)I;", name.object<jstring>(), defId);

    W/System.err(10285): java.lang.NoSuchMethodError: no non-static method "Landroid/app/SharedPreferencesImpl;.getInt(Ljava/lang/String;I)I;"
    W/System.err(10285): at org.qtproject.qt5.android.QtNative.startQtApplication(Native Method)
    W/System.err(10285): at org.qtproject.qt5.android.QtNative$6.run(QtNative.java:365)
    W/System.err(10285): at org.qtproject.qt5.android.QtThread$1.run(QtThread.java:61)
    W/System.err(10285): at java.lang.Thread.run(Thread.java:818)

    I also don't understand the semantic issue:
    warning: instantiation of function QAndroidJniObject::callMethod required here, but no defenition is available

    Does someone know whats going wrong here? Thxs...



  • Now it works, the reason was the semicolon at the end which I overlooked.

    "(Ljava/lang/String;I)I;" -> "(Ljava/lang/String;I)I"


Log in to reply