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.


    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);


    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 Method)
    W/System.err(10285): at$
    W/System.err(10285): at$
    W/System.err(10285): at

    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