title: Accessing java variable from native code via JNI giving junk value
body: <p>I'm creating a video player in android. In my android side of the code i have a toggle button for play/pause state. If plafFlag=0, then the video needs to be paused (playFlag is a variable in java part of the code).</p>
  
  <p>In the native side of the code i'm trying to access the value of this playFlag each before each time a frame is displayed onto the screen.</p>
  
  <p>Below is my java part the code:</p>
  
  <pre><code>public class myPlayer extends Activity {
      public int playFlag;
     .
     .
     .
     .
     protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.player);
            final ToggleButton playFlagBtn=(ToggleButton) findViewById(R.id.toggleButton1);
  
           playFlagBtn.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
                  // TODO Auto-generated method stub
                  if(playFlagBtn.isChecked()){
                      playFlag=0;
                  }
                  else{
                      playFlag=1;
                  }
              }
          });
  </code></pre>
  
  <p>From this <strong>OnCreate</strong> function itself i have called the native code where i'm trying to access <strong>playFlag</strong> variable.</p>
  
  <p>Below is the code snippet using which i'm accessing the <strong>playFlag</strong> variable:</p>
  
  <pre><code>jclass cls = (*env)-&gt;FindClass(env,"com/example/my_decoder/myPlayer");  __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","reference to class obatined",NULL);
  
   jfieldID fidInt=(*env)-&gt;GetFieldID(env,cls,"playFlag","I");
   __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","fid obtained",NULL);
  
   int Flag=0;
  
  __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","Before fetching flag value",NULL);
  playFlag=(*env)-&gt;GetIntField(env,jobj,fidInt);
  __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","Initial value of playflag=%d",playFlag);
  </code></pre>
  
  <p>The above native code snippet doesnt give an error. The value is fetched, but it seems like some junk value.</p>
  
  <p>In the logcat i get output like:</p>
  
  <pre><code>03-08 11:21:32.627: D/MYAPP(31220): Initial value of playflag=1085053248
  </code></pre>
  
  <p>The main intention here is to pause the decoding process if the value of the fetched flag is 0., otherwise continue to decode the frames and render it on the screen. Hence for me it is very important to fetch the right value of the flag from the java part of the code.</p>
  
  <p>Can anyone please explain where i'm going wrong. Or if there is another way to do it.</p>
  
  <p>Waiting for some replies. Thanks in advance.</p>