无法访问c++;java中的对象并获得错误的结果c++;android studio中跨JNI层的长objectptr函数 C++ C++方法,我尝试通过创建一个C++类的指针,将它存储在java中的长值中,作为Java类中的数据成员,并试图从通过传递长值所创建的对象访问C+C++方法并将其传递给我想调用的本机方法。我正在将其转换为本机方法中的Numbers类,即我要访问其方法的类。 但输出错误。我猜是垃圾价值。我对这个概念完全陌生。谁能告诉我确切的问题或纠正错误,如果作出

无法访问c++;java中的对象并获得错误的结果c++;android studio中跨JNI层的长objectptr函数 C++ C++方法,我尝试通过创建一个C++类的指针,将它存储在java中的长值中,作为Java类中的数据成员,并试图从通过传递长值所创建的对象访问C+C++方法并将其传递给我想调用的本机方法。我正在将其转换为本机方法中的Numbers类,即我要访问其方法的类。 但输出错误。我猜是垃圾价值。我对这个概念完全陌生。谁能告诉我确切的问题或纠正错误,如果作出,java,c++,java-native-interface,Java,C++,Java Native Interface,现在,我正在尝试创建一个非常简单的数字类,它支持对两个数字的简单操作,add、mul、sub和一个构造函数来初始化数字-a和b。 请查找各种类别的代码: MainActivity.java package com.example.maths; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class MainA

现在,我正在尝试创建一个非常简单的数字类,它支持对两个数字的简单操作,add、mul、sub和一个构造函数来初始化数字-a和b。 请查找各种类别的代码:

MainActivity.java

package com.example.maths;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private long  numberptr = 0; //c++ object reference

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Creating Number object
        numberptr = createNumber();

        //Performing add operation
        int result = nativeAdd(numberptr);

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text );
        tv.setText(stringFromJNI() + "result is " + result);
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    //Native method to create instance of Number (c++ class) and store it in java in numberptr long object
    public native long createNumber();

    public native int nativeAdd(long numberptr);
}
Native-lib.cpp

#include <jni.h>
#include <string>
#include "Numbers.h"

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_maths_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT jlong JNICALL
Java_com_example_maths_MainActivity_createNumber(JNIEnv *env, jobject thiz) {
    // TODO: implement createNumber()
    return reinterpret_cast<jlong>(new Numbers(3, 4));
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_example_maths_MainActivity_nativeAdd(JNIEnv *env, jobject thiz, jlong numberptr) {
    // TODO: implement nativeAdd()
    Numbers* num = reinterpret_cast<Numbers *>(numberptr);
    return num->add();
}
数字.cpp

#include "Numbers.h"

Numbers::Numbers(int a, int b) {
    a = a;
    b = b;
}

int Numbers::add() {
    return a+b;
}

int Numbers::mul() {
    return a*b;
}

int Numbers::sub() {
    return a-b;
}
CmakeLists.txt

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             native-lib.cpp
             Numbers.cpp
        )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

请告诉我是什么问题,或者应该如何处理。提前谢谢。

您的数字构造函数没有分配给对象字段。@Bojte,对不起,我没有得到。我试图分配我传递给它的值a=3和b=4。我没有将值指定给对象字段。如果我错了,请纠正我。你能举例说明吗?我到底应该怎么做?你是在给自己分配参数。使用a或显式执行
this->a=a您的数字构造函数没有分配给对象字段。@Bojte,对不起,我没有得到。我试图分配我传递给它的值a=3和b=4。我没有将值指定给对象字段。如果我错了,请纠正我。你能举例说明吗?我到底应该怎么做?你是在给自己分配参数。使用a或显式执行
this->a=a
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             native-lib.cpp
             Numbers.cpp
        )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )