如何通过QtAndroid::androidActivity().callObjectMethod获取int值?
Solution for Retrieving int/jint from Android Activity in Qt 5.12
Let’s fix that compilation error right away—you just picked the wrong QAndroidJniObject method for primitive data types!
The core issue here is mixing up methods designed for object types vs primitive types:
callObjectMethod<T>is exclusively for Java methods that return objects (likejstring, custom class instances, etc.). Sinceint/jintare primitive data types in Java, this method throws a compile error because it expects to wrap an object, not a raw primitive value.- For primitive return types such as
jint, you need to use thecallMethod<T>method instead.
Correct Code Implementation
Here’s the proper way to call your Java method that returns an int:
// Call the Java method that returns a jint jint my_jint = QtAndroid::androidActivity().callMethod<jint>("someMethod"); // Convert to Qt's native int type (safe, since jint maps directly to int on most platforms) int my_int = static_cast<int>(my_jint);
Handling Methods With Parameters
If your someMethod accepts arguments, you’ll need to pass the method signature and parameters just like you would with object methods. For example, if your Java method looks like this:
public int someMethod(String input) { // Your logic here return 42; }
Your Qt code would look like this:
jint result = QtAndroid::androidActivity().callMethod<jint>( "someMethod", "(Ljava/lang/String;)I", // Method signature: takes a String, returns int QAndroidJniObject::fromString("Hello from Qt").object<jstring>() );
Quick Notes
- This API behavior is identical across Qt 5.12 Commercial and Open Source editions—no special differences here.
jintis just a JNI typedef for Java’sint, so casting it to Qt’s nativeintis straightforward and safe for all standard Android target architectures.
内容的提问来源于stack exchange,提问作者AdeleGoldberg
相关产品推荐
相关产品推荐

