Discussion:
How to how to return a integer value from cpp file to Java class
zachary lim
2018-04-01 12:24:12 UTC
Permalink
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();
}
--
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+***@googlegroups.com.
To post to this group, send email to android-***@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.
mic _
2018-04-02 15:47:04 UTC
Permalink
Post by zachary lim
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.
Post by zachary lim
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!
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 {
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();
}
--
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
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
<https://groups.google.com/d/msgid/android-ndk/683c6dbf-41e7-432d-b0ad-683b42ecec47%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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+***@googlegroups.com.
To post to this group, send email to android-***@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/CAJwpw3bd%3DBJMqzU2y1CW%2BW5tjzhS_pA0vzVVZ%2BvgybTZBUSPWQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
zachary lim
2018-04-04 09:57:20 UTC
Permalink
Oh drats, i forgot to create the function caller /flipstable

Thank you very much!
--
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+***@googlegroups.com.
To post to this group, send email to android-***@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/4e81ab73-174b-4b3c-b367-d4ff41011d09%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...