title: Accessing variables of Activity on Callback in Android
body: <p>I want to access a global variable which is initialized in OnCreate through a callback which is returned. </p>
  
  <p>The complications :</p>
  
  <ol>
  <li><p>The callbacks are from native c++ code, so I pass in an object of the activity in initialize function so that the C++ code callback the Java code. This works perfectly fine as I am able to log when my Java method is called from C++</p></li>
  <li><p>Now when I get the callback from C++ code, I try to call a method of an object named pc.(PeerConnection). But now when the callback is received pc is always null, thus throwing a null pointer exception. </p></li>
  </ol>
  
  <p>I think it is due to the fact that the instance of the Activity I pass and the instance where pc is initialized are different, I even tried passing context/activity instance to C++ code but that doesn't work  either. </p>
  
  <p>Any ideas on what I may be doing wrong?</p>
  
  <p>Here is my Activity code :</p>
  
  <pre><code>   @Override
         protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              initialize(new MainActivity());   // This is native function which takes the object name
  
              PeerConnectionFactory factory = new PeerConnectionFactory();
              MediaConstraints pcConstraints = sdpMediaConstraints;
              pcConstraints.optional.add(
              new MediaConstraints.KeyValuePair("RtpDataChannels", "true"));
              pcConstraints.optional.add(
              new MediaConstraints.KeyValuePair("DtlsSrtpKeyAgreement", "true"));
  
              pc = factory.createPeerConnection(iceServers, pcConstraints, pcObserver); // This is where pc is assigned 
  
         }
  
         //Callback from the C++ code
         public void onConnected(){
  
               Log.d("Log", "Came in onConnected state from C++ " ); // This log is printed and works fine
               pc.setSdp();    // This is where it fails, it just doesn't find pc 
  
  
         }
  </code></pre>
  
  <p>Any ideas what I can do to have access to that pc variable in the callback ? I already tried storing it as a static variable and accessing it later but that doesn't work either. </p>
  
  <p>Edit :</p>
  
  <p>Logcat : <a href="http://paste2.org/A0ea06wK" rel="nofollow">http://paste2.org/A0ea06wK</a></p>
  
  <p>Edit 2:</p>
  
  <p>So I figured what I was doing wrong, I had a service and an AIDL so there were 2 different process thus the variables were null no matter what. </p>
  
  <p>I still don't know how to access the variables of one process after I recieve a callback from the other process</p>