Log in or register to post comments

Passing one variable from java to c++

April 18, 2013 - 9:45am #1

In my project, I have to pass my intiger variable from my one java class to ImageTarget.cpp class. Is there any way to see these variable's value from ImageTarget.cpp class. Because I have a loop game and each turn this variable has different value. And I have to see this variable's valur from ImageTarget.cpp class.

Thanks for you help.

Passing one variable from java to c++

April 20, 2013 - 12:05pm #22

Glad to help.

Passing one variable from java to c++

April 20, 2013 - 7:45am #21

OK, I solve the problem. Thank you very much for your help. You are the best.

Passing one variable from java to c++

April 19, 2013 - 8:02am #20

Have you called setImageTargetsRef() method passing the ImageTargets instance before running the other code ?

 

Passing one variable from java to c++

April 19, 2013 - 7:48am #19

Thank you for your help but when I do that it gives an error like that;

04-19 17:43:12.880: E/AndroidRuntime(17427): FATAL EXCEPTION: main
04-19 17:43:12.880: E/AndroidRuntime(17427): java.lang.NullPointerException
04-19 17:43:12.880: E/AndroidRuntime(17427): at org.example.treasurehuntnew.Puzzle$1.onClick(Puzzle.java:41)
04-19 17:43:12.880: E/AndroidRuntime(17427): at android.view.View.performClick(View.java:3549)
04-19 17:43:12.880: E/AndroidRuntime(17427): at android.view.View$PerformClick.run(View.java:14393)
04-19 17:43:12.880: E/AndroidRuntime(17427): at android.os.Handler.handleCallback(Handler.java:605)
04-19 17:43:12.880: E/AndroidRuntime(17427): at android.os.Handler.dispatchMessage(Handler.java:92)
04-19 17:43:12.880: E/AndroidRuntime(17427): at android.os.Looper.loop(Looper.java:154)
04-19 17:43:12.880: E/AndroidRuntime(17427): at android.app.ActivityThread.main(ActivityThread.java:4944)
04-19 17:43:12.880: E/AndroidRuntime(17427): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 17:43:12.880: E/AndroidRuntime(17427): at java.lang.reflect.Method.invoke(Method.java:511)
04-19 17:43:12.880: E/AndroidRuntime(17427): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-19 17:43:12.880: E/AndroidRuntime(17427): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-19 17:43:12.880: E/AndroidRuntime(17427): at dalvik.system.NativeStart.main(Native Method)
 

Passing one variable from java to c++

April 19, 2013 - 7:25am #18

You need to pass a reference to ImageTargets class to your calling class, store the reference in a member variable of your class and then call it like this (suppose your class is called "MyClass"):

public class MyClass
{
  ImageTargets mImageTargets = null;


  public void setImageTargetsRef( ImageTargets imageTargets ) {

    mImageTargets = imageTargets;

  }


}

and then you can call (from your class code):

mImageTargets.deinitQCAR();

Note: this is just about Java programming...

 

Passing one variable from java to c++

April 19, 2013 - 7:21am #17

Hi, I did what you said but I cant reach this method from another class. As you said i wrote it public but I cant reach it with these two commands;

deinitQCAR();
or
ImageTargets.deinitQCAR();

Passing one variable from java to c++

April 19, 2013 - 6:46am #16

Hi,

you can use the same de-initialization code that you find in ImageTargets.java;

if you need to call it from a different class, you can create a public method in ImageTargets.java, for instance 

public void deinitQCAR() 

{

// put the deinit code here,

// same as the code that you find in onDestriy() in ImageTargets

}

And then you can call that method from any other class.

Passing one variable from java to c++

April 19, 2013 - 4:53am #15

How can I deinitialize QCAR in the other class? With this command?

QCAR.deinit();

Intent intent=new Intent(Puzzle.this,ImageTargets.class);

startActivity(intent);
 
But now I have an error like this;
04-19 14:51:56.052: E/AndroidRuntime(15016): FATAL EXCEPTION: main
04-19 14:51:56.052: E/AndroidRuntime(15016): java.lang.UnsatisfiedLinkError: deinit
04-19 14:51:56.052: E/AndroidRuntime(15016): at com.qualcomm.QCAR.QCAR.deinit(Native Method)
04-19 14:51:56.052: E/AndroidRuntime(15016): at org.example.treasurehuntnew.Puzzle$1.onClick(Puzzle.java:47)
04-19 14:51:56.052: E/AndroidRuntime(15016): at android.view.View.performClick(View.java:3549)
04-19 14:51:56.052: E/AndroidRuntime(15016): at android.view.View$PerformClick.run(View.java:14393)
04-19 14:51:56.052: E/AndroidRuntime(15016): at android.os.Handler.handleCallback(Handler.java:605)
04-19 14:51:56.052: E/AndroidRuntime(15016): at android.os.Handler.dispatchMessage(Handler.java:92)
04-19 14:51:56.052: E/AndroidRuntime(15016): at android.os.Looper.loop(Looper.java:154)
04-19 14:51:56.052: E/AndroidRuntime(15016): at android.app.ActivityThread.main(ActivityThread.java:4944)
04-19 14:51:56.052: E/AndroidRuntime(15016): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 14:51:56.052: E/AndroidRuntime(15016): at java.lang.reflect.Method.invoke(Method.java:511)
04-19 14:51:56.052: E/AndroidRuntime(15016): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-19 14:51:56.052: E/AndroidRuntime(15016): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-19 14:51:56.052: E/AndroidRuntime(15016): at dalvik.system.NativeStart.main(Native Method)
 

 

Passing one variable from java to c++

April 19, 2013 - 4:42am #14

Ok, but then you should simply deinitialize QCAR when you start "puzzle" (but not calling the entire onDestroy());

Passing one variable from java to c++

April 19, 2013 - 4:36am #13

But when I dont call onDestroy function I have this situation;

I have a game project like this.

puzzle -> imageTargets -> question -> puzzle  -> imageTargets -> question ... and it goes on like this. (these are my classes and on the screen there are a button to switch between them)

but when 2nd puzzle come, than go to the  imageTargets class i have an error like that on telephone screen;

Failed to initialize QCAR.

and also I have an error like this on logcat;

 

E/QCAR(31031): InitQCARTask::onPostExecute: Failed to initialize QCAR. Exiting.
 
I searched on the forum and found a solution something like adding timer but I addded a timer to some classes but the error still continues.
What can I do? 

 

Thanks..

 

Passing one variable from java to c++

April 19, 2013 - 4:27am #12

Hi, you should never call onDestroy() explicitly; just start the new activity.

 

Passing one variable from java to c++

April 19, 2013 - 4:23am #11

Thank you very much I did it but now I have a problem when calling onDestroy() function  think. In logcat I have an error like this;

04-19 14:17:22.520: I/QCAR(6176): Successfully destroyed the data set tv.
04-19 14:17:22.520: I/QCAR(6176): Java_org_example_treasurehuntnew_ImageTargets_deinitTracker
04-19 14:17:22.550: A/libc(6176): Fatal signal 11 (SIGSEGV) at 0x00000004 (code=1)
04-19 14:17:22.721: W/SurfaceTexture(6176): freeAllBuffersExceptCurrentLocked called but mQueue is not empty
04-19 14:17:22.801: D/dalvikvm(6176): GC_EXPLICIT freed 12568K, 83% free 4231K/24867K, paused 2ms+5ms

In my program, when marker detected and shown the object, user double press the screen then go to other page. When doing that I called onDestroy() function. like this. Is there any problem?

onDestroy();
Intent intent=new Intent(ImageTargets.this,Question.class);
startActivity(intent);
 

Passing one variable from java to c++

April 19, 2013 - 2:10am #10

Hi, maybe try to initialize your marker variable to some value, for instance (in your global declaration):

int marker = -1;

Actually, there is an example of this also in ImageTargets.cpp and ImageTargets.java, see the method setActivityPortraitMode() and boolean variable isActivityPortraitMode

The only difference is that it is a boolean, instead of an int, nothing more.

 

Passing one variable from java to c++

April 19, 2013 - 1:51am #9

Actually I want to assign coming integer value to my cpp variable. I dont want to print it. 
I decleared an integer variable like this in cpp;

...

 

int textureCount                = 0;

 

Texture** textures              = 0;
 
int marker; //this is my decleration (and also I tried to declare as jint marker;)

...

And in fuction I assigned like that;

...

JNIEXPORT void JNICALL
Java_org_example_treasurehuntnew_ImageTargets_printInteger(JNIEnv *, jobject, jint value)
{
marker = value;
}
...

In java part I passed the variable as an integer.
...
private native void printInteger( int umut );

...

When I do that I have an error in logcat like this;

 A/libc(18905): Fatal signal 11 (SIGSEGV) at 0x00000004 (code=1)
 

 

Passing one variable from java to c++

April 19, 2013 - 1:25am #8

Hi,

if your goal is to print an integer value (i.e. if your Java variable is an integer, as I understood from your original post), then you simply need to adjust the printInteger function code as follows:

JNIEXPORT void JNICALL
Java_org_example_treasurehuntnew_ImageTargets_printInteger(
                       JNIEnv *, jobject, jint value)
{
LOG("Hello, this is my integer  %d",  value);
}
 
As you can see, I just replaced "%s" with "%d" 
 
If, conversely, your Java variable is a String, and you want to pass it to C++ and print it in C++, then you need to change the native method declaration in Java:
 
private native void printString( String myJavaString );

 

and you need to define this function in C++:
 
JNIEXPORT jboolean JNICALL
Java_org_example_treasurehuntnew_ImageTargets_printString(
                 JNIEnv *, jobject, jstring myJavaString)
{
  const char *cstr = env->GetStringUTFChars( myJavaString, 0 );
  LOG("Hello, this is my string  %s",  cstr);
  env->ReleaseStringUTFChars(myJavaString, cstr);
}

 

 

 

Passing one variable from java to c++

April 19, 2013 - 12:56am #7

Hi, I did what you said but I have a problem like this now;

I have to convert string to integer in this function; (marker is my global integer in cpp)
(I think that variable named value is String because of %s)

 

JNIEXPORT void JNICALL
Java_org_example_treasurehuntnew_ImageTargets_printInteger(JNIEnv *, jobject, jint value)
{
LOG("Hello, this is my integer  %s",  value);
}
 
I found 3 solution in internet but noone works (gives error when ndk build)
1)marker = atoi(value);
 
2)std::string myString = value;
    int realValue = atoi(myString.c_str());
    marker = realValue;
 
3)marker = convertString(value);
 
What will I do now?
Thanks

 

 

 

Passing one variable from java to c++

April 18, 2013 - 10:39pm #6

Hi, you also need to call the printInteger() function somewhere in your Java code, i.e. you need to put a line like this somewhere:

printInteger ( umut );

 

Passing one variable from java to c++

April 18, 2013 - 3:51pm #5

I have access my variable with this sentence from all of my java class;
MainScreen.number.marker

now I want to pass it ImageTarget.cpp
I add a command like this to my ImageTarget.java class;
...
private int mScreenWidth = 0;
private int mScreenHeight = 0;
private int umut = MainScreen.number.marker;
...

And I add a command like this to ImageTarget.java class;
...

 

private native void stopCamera();

 

private native void setProjectionMatrix();
private native void printInteger( int umut );
...
 
And also I add this commands to ImageTarget.cpp
...
int textureCount                = 0;
Texture** textures              = 0;
int marker;
...
JNIEXPORT void JNICALL
Java_org_example_treasurehuntnew_ImageTargets_printInteger(JNIEnv *, jobject, jint value)
{
LOG("Hello, this is my integer  %s",  value);
    marker = value;
}
...

But I think that still I cant access to my java variable.
What will I do?
Thanks for your help..

 

Passing one variable from java to c++

April 18, 2013 - 2:25pm #4

Ok, so, if I understand correctly, you want to know how to implement a native method to pass down a variable from Java to C++, right ?

That's quite simple to do:

suppose your class is ImageTargets (ImageTargets.java), and you want to pass an integer to C++, what you can do is to declare a native method in your Java class (ImageTargets.java in our example), like this:

 

private native void printInteger( int value );
 
Then, in ImageTargets.cpp, you will define a function like this:
 
JNIEXPORT void JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargets_printInteger(JNIEnv *, jobject, jint value)
{
    LOG("Hello, this is my integer  %s",  value);
}

 

 

Passing one variable from java to c++

April 18, 2013 - 10:16am #3

But I have the catch Java class's integer value than use it in the ImageTarget.cpp class.

Passing one variable from java to c++

April 18, 2013 - 9:50am #2

Hi,

one simple way is to Log the variable using the LOG function (see examples of usage of the LOG function in ImageTargets.cpp;

for instance to log an integer, you can write:

LOG( "My integer is: %d ", myIntegerVariable);

Then you can see the values in Eclipse (in the Log console) or using ADB logcat;

for more information on how to use adb logcat, refer to this page:

http://developer.android.com/tools/help/logcat.html

 

 

Log in or register to post comments