How to how to return a integer value from cpp file to Java class

210 views
Skip to first unread message

zachary lim

unread,
Apr 2, 2018, 11:28:01 AM4/2/18
to android-ndk
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();
}

mic _

unread,
Apr 2, 2018, 11:47:18 AM4/2/18
to andro...@googlegroups.com
int hello4 = integer;

How does that even compile? You left out the parentheses for the method call.

If you are indeed getting a runtime error, you should start by inspecting the stacktrace from logcat.


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/683c6dbf-41e7-432d-b0ad-683b42ecec47%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

zachary lim

unread,
Apr 4, 2018, 10:43:25 AM4/4/18
to android-ndk
Oh drats, i forgot to create the function caller /flipstable

Thank you very much!
Reply all
Reply to author
Forward
0 new messages