Android Studio项目: 我试图通过从java传递变量(integer,double ...)来调用C ++类对象,并使用JNI接口从c ++类对象返回结果(double)。当我编译/构建项目时,没有错误或警告,它编译得很好。但是,当我运行应用程序时,它崩溃了“不幸的是,App_xxx已停止”。我已经将错误跟踪到我调用对象的方式(在java和NTI文件中)。
我是否从MainActivity和JNI界面中正确调用了对象?任何人都可以帮助或查看我的代码错误吗?
这是MainActivity java的代码:
package com.abc.www.apps;
import ...
public class MainActivity extends AppCompatActivity {
double ax, bx, cx;
double totalA, totalB;
// 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);
//Call object in .cpp file (C++)
ax = 2;
bx = 3;
cx = 4;
totalA = getSum(ax, bx, cx);
totalB = getDiff(ax, cx);
//Display results
TextView tv = (TextView) findViewById(R.id.id_dispTotA);
tv.setText(Double.toString(totalA));
TextView tv = (TextView) findViewById(R.id.id_dispTotB);
tv.setText(Double.toString(totalB));
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native double getSum(double ax, double bx, double cx);
public native double getDiff(double ax, double cx);
}
这是native-lib.cpp的代码:
#include <jni.h>
#include <string>
#include "mathResult.h"
extern "C"
JNIEXPORT jdouble JNICALL
Java_com_abc_www_apps_MainActivity_getSum(
JNIEnv *env, jobject,
jfloat a, jfloat b, jfloat c) {
jdouble value;
mathResult m;
value = m.getSum(a, b, c);
return value;
}
JNIEXPORT jdouble JNICALL
Java_com_abc_www_apps_MainActivity_getDiff(
JNIEnv *env, jobject,
jfloat a, jfloat c) {
jdouble value;
mathResult m;
value = m.getDiff(a, c);
return value;
}
这是C ++类对象(.cpp)的代码:
#include "mathResult.h"
#include "math.h"
//Sum
double mathResult::getSum(double a, double b, double c)
{
Sum = a + b + c;
return Sum;
}
//Subtraction
double mathResult::getDiff(double a, double c)
{
Diff = c - a;
return Diff;
}
非常感谢任何帮助,谢谢。
答案 0 :(得分:0)
MainActivity
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
double ax, bx, cx;
double totalA, totalB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ax = 2;
bx = 3;
cx = 4;
totalA = getSum(ax, bx, cx);
totalB = getDiff(ax, cx);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(Double.toString(totalA) + " === " + Double.toString(totalB));
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public native double getSum(double ax, double bx, double cx);
public native double getDiff(double ax, double cx);
}
<强> Nativelib.cpp 强>
#include <jni.h>
#include <string>
#include "math.h"
extern "C"
JNIEXPORT jstring
JNICALL
Java_com_abc_www_apps_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT jdouble JNICALL
Java_com_abc_www_apps_MainActivity_getSum(JNIEnv *env, jobject instance, jdouble ax,
jdouble bx, jdouble cx) {
return math().getSum(ax, bx, cx);
}extern "C"
JNIEXPORT jdouble JNICALL
Java_com_abc_www_apps_MainActivity_getDiff(JNIEnv *env, jobject instance, jdouble ax,
jdouble cx) {
return math().getDiff(ax, cx);
}
<强> math.h中强>
#ifndef JNIDEMO_MATH_H
#define JNIDEMO_MATH_H
#include <jni.h>
class math {
public:
jdouble getSum(jdouble ax, jdouble bx, jdouble cx);
jdouble getDiff(jdouble ax,jdouble bx);
};
#endif //JNIDEMO_MATH_H
math.cpp
#include <jni.h>
#include "math.h"
jdouble math::getSum(jdouble ax, jdouble bx, jdouble cx) {
return ax+bx+cx;
}
jdouble math::getDiff(jdouble ax, jdouble bx) {
return ax-bx;
}
注意:
例如,您必须在CmakerLists.txt
中添加新添加的文件
math.cpp 因此请更改cmakerLists.txt
代码,如下所示添加文件
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).
src/main/cpp/native-lib.cpp
src/main/cpp/math.cpp)
这里我添加了src / main / cpp / math.cpp和add_library