title: Unable to get instance of Activity in Unity C#
body: <p>I've been trying to access an Activity method from Unity and cannot figure out for the life of me why it is not working. I've read various pages such as <a href="http://forum.unity3d.com/threads/call-a-method-in-an-activity-using-androidjavaobject.219950/" rel="nofollow">http://forum.unity3d.com/threads/call-a-method-in-an-activity-using-androidjavaobject.219950/</a> and <a href="http://docs.unity3d.com/460/Documentation/Manual/PluginsForAndroid.html" rel="nofollow">http://docs.unity3d.com/460/Documentation/Manual/PluginsForAndroid.html</a> </p>
  
  <p>I'm trying to create a Camera plugin for Unity. There are some plugins out there, but most are outdated and don't work. I could use the knowledge anyway for future use. I was originally just using a regular Java class, but to take a picture, I discovered that the best route was to use an Intent due to the fact that it's being used for quick photos, not a personalized camera application. To use an Intent, an Activity must be used. The Activity is passed to the Intent, but the Activity also has to override a method to receive the result from the Intent as outlined in Android documentation. I would link to it but I'm limited to 2 links.</p>
  
  <p>I'm using this to access the activity. The AndroidJavaClass initializes successfully, however, when I try and get the context, I get a null ptr error in logcat.</p>
  
  <h1>C# code</h1>
  
  <pre><code>    AndroidJavaClass link = new AndroidJavaClass("com.syncedsoftware.cameralink.CameraLinkActivity");
      AndroidJavaObject activity = link.Call&lt;AndroidJavaObject&gt;("getLinkContext"); // jni null ptr!
  
      activity.Call("TakePicture");
  </code></pre>
  
  <p>It never reaches activity.Call("TakePicture");.</p>
  
  <h1>Java class -UPDATED WITH STATICS-</h1>
  
  <pre><code>public class CameraLinkActivity extends UnityPlayerActivity {
  
  private static Context cameraLinkContext;
  
  public static Context getLinkContext(){
      Log.v("Unityx", "GetLinkContext()");
      return cameraLinkContext;
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Log.v("Unity", "OnCreate");
      cameraLinkContext = this;
  }
  
  public void TakePicture(){
      Log.v("Unity", "TakePicture()");
  }
  }
  </code></pre>
  
  <h1>Logcat output -UPDATED WITH NEW STATIC IMPLEMENTATION-</h1>
  
  <pre><code>05-29 12:32:50.560    3408-3498/? V/Unityx﹕ GetLinkContext()
  05-29 12:32:50.810    3408-3498/? I/Unity﹕ Exception: JNI: Init'd AndroidJavaObject with null ptr!
          at UnityEngine.AndroidJavaObject..ctor (IntPtr jobject) [0x00000] in &lt;filename unknown&gt;:0
          at UnityEngine.AndroidJavaObject.AndroidJavaObjectDeleteLocalRef (IntPtr jobject) [0x00000] in &lt;filename unknown&gt;:0
          at UnityEngine.AndroidJavaObject._CallStatic[AndroidJavaObject] (System.String methodName, System.Object[] args) [0x00000] in &lt;filename unknown&gt;:0
          at UnityEngine.AndroidJavaObject.CallStatic[AndroidJavaObject] (System.String methodName, System.Object[] args) [0x00000] in &lt;filename unknown&gt;:0
          at AndroidCameraLink.Start () [0x00000] in &lt;filename unknown&gt;:0
  </code></pre>
  
  <p><strike>Something I noticed is that the message in onCreate() is never displayed. This leads me to believe that the Activity is not being properly created or created at all.</strike> The Activity is never being "created", thus onCreate() is never called to initialize the Context field.</p>
  
  <h1>AndroidManifest.xml</h1>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.syncedsoftware.cameralink"     android:theme="@android:style/Theme.NoTitleBar" android:versionName="1.0" android:versionCode="1" android:installLocation="preferExternal"&gt;
    &lt;supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" /&gt;
    &lt;application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false"&gt;
    &lt;activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale"&gt;
        &lt;intent-filter&gt;
          &lt;action android:name="android.intent.action.MAIN" /&gt;
          &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
        &lt;/intent-filter&gt;
        &lt;meta-data android:name="unityplayer.UnityActivity" android:value="true" /&gt;
        &lt;meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" /&gt;
      &lt;/activity&gt;
  &lt;activity android:name="com.syncedsoftware.cameralink.CameraLinkActivity" /&gt; 
        &lt;/application&gt;
        &lt;uses-sdk android:minSdkVersion="9" android:targetSdkVersion="18" /&gt;
        &lt;uses-feature android:glEsVersion="0x00020000" /&gt;
        &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
        &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
  &lt;uses-feature android:name="android.hardware.camera" /&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>If someone could explain why this is not working, I would be very grateful. I've been trying to figure this out for almost a week now, to no avail. This is my first time treading into native territory with Unity, please excuse my ignorance.</p>
  
  <h1>UPDATE: getLinkContext() is being called, but the problem persists.</h1>
  
  <p>After flipping the implementation to static, I discovered that I was overlooking the logcat message. When flipping it to static, getLinkContext() is successfully called, however, this doesn't fix the original issue of not being able to get the instance of the Activity.</p>
  
  <p>cameraLinkContext is null because onCreate() is never called. The Activity is never being "created".</p>