hi all, I am new here as well as new to Android C++ JNI
Essentially my problem is this: I want to display start-up.cpp into the Toast, everything else works until i added useful_cpp then it crashes, may i know what went wrong?
every step i take, i go backwards 2 times, every time i think i got the concept, the next time i demonstrate it, it crashes again, it is actually like redoing C++ from scratch all over again lol, thanks for your help and please be patient with me, thank you!
These are my details:
start-up.h---------
using std::endl;
using std::printf;
using std::string;
using std::stringstream;
#ifndef NDKTEST_USEFUL_CPP_H
#define NDKTEST_USEFUL_CPP_H
class useful_cpp {
public:
useful_cpp()
{
}
int Result()
{
int output1 = 1;
int output2 = 2;
int result = output1 + output2;
//output = result;
//const char *res = output.c_str();
return result;
}
};
#endif //NDKTEST_USEFUL_CPP_H
start-up.cpp--------
#include <jni.h>
#include <string>
#include "useful_cpp.h"
using std::string;
using std::stringstream;
extern "C"
JNIEXPORT jint JNICALL Java_beginnerapps_MainActivity_integer(JNIEnv *env, jobject /* this */)
{
useful_cpp usefulCpp;
int returnCpp = usefulCpp.Result();
return returnCpp;
}
MainActivity.java---------------
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
//load the library - name matches jni/Android.mk
System.loadLibrary("native-lib");
System.loadLibrary("ndkfoo");
System.loadLibrary("HelloWorld");
System.LoadLibrary("useful_cpp");
}
public native String helloWorld();
public native String string();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
//this is where we call the native code
String hello1 = stringFromJNI();
String hello2 = string();
String hello3 = helloWorld();
int hello4 = integer;
String intHello = Integer.toString(hello4);
tv.setText(hello1 + "\n" + hello2 + "\n" + hello3 + "\n" + intHello + "\n");
Toast.makeText(this, hello1 + "\n" + hello2 + "'\n" + hello3 + "\n", Toast.LENGTH_LONG).show();
//create AlertDialog
new AlertDialog.Builder(this).setMessage(hello1 + "\n" + hello2 + "'\n" + hello3 + "\n").show();
}
public native String stringFromJNI();
public native int integer();
}