Search Unity

[SOLVES]Exception: JNI: Init'd AndroidJavaClass with null ptr!

Discussion in 'Android' started by Dzxyan, Jul 22, 2015.

  1. Dzxyan

    Dzxyan

    Joined:
    Sep 23, 2013
    Posts:
    165
    I'm try to write this on example.cs script

    Code (CSharp):
    1. void Start()
    2. AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    3. }
    it's always return the error "Exception: JNI: Init'd AndroidJavaClass with null ptr!";
    I'm sure my platform have switch to android and i'm try to build in apk and run also get same error,
    im using unity3d 5.1.1
    any help for that?
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,350
    Are you running this code in the editor? you cannot use the AndroidJavaClass or AndroidJavaObject classes while in the editor. These classes rely on actually running on an Android device with a JVM.
     
  3. Dzxyan

    Dzxyan

    Joined:
    Sep 23, 2013
    Posts:
    165
    im try in editor and android also
    both got same error
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,350
    Are you sure tge error is thrown by the code you attached?

    Also, could you share the APK so i could peek inside ? (In private).

    I would like to see if it contains the UnityPlayer class.
     
  5. Dzxyan

    Dzxyan

    Joined:
    Sep 23, 2013
    Posts:
    165
  6. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,350
    Which line fails exactly in your code?
     
  7. Dzxyan

    Dzxyan

    Joined:
    Sep 23, 2013
    Posts:
    165
    Code (CSharp):
    1. AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    maybe can say that all define as AndroidJavaClass will fails
     
  8. Dzxyan

    Dzxyan

    Joined:
    Sep 23, 2013
    Posts:
    165
    Problem solves,
    my plugin class read wrong class
     
    KeOt777 and liortal like this.
  9. KeOt777

    KeOt777

    Joined:
    Feb 29, 2016
    Posts:
    1
    What exactly was the problem? How did you fix your code? I'm having the same problem you are but since you didn't publish the code correction I have no idea what you did to fix this, care to share?

    Thanks!
     
  10. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,350
    @KeOt777 what is the code you're using? can you share it ?
     
  11. rashedn

    rashedn

    Joined:
    Feb 24, 2017
    Posts:
    1
    Exception: JNI: Init'd AndroidJavaClass with null ptr!
    UnityEngine.AndroidJavaClass..ctor (IntPtr jclass) (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:556)
    UnityEngine.AndroidJavaObject.get_JavaLangClass () (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:534)
    UnityEngine.AndroidJavaObject.FindClass (System.String name) (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:525)
    UnityEngine.AndroidJavaClass._AndroidJavaClass (System.String className) (at C:/buildslave/unity/build/Runtime/Export/AndroidJavaImpl.cs:545)
    UnityEngine.AndroidJavaClass..ctor (System.String className) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/AndroidJavaBindings.gen.cs:94)
    AudienceNetwork.InterstitialAdBridgeAndroid.Create (System.String placementId, AudienceNetwork.InterstitialAd interstitialAd) (at Assets/AudienceNetwork/Library/InterstitialAd.cs:375)
    AudienceNetwork.InterstitialAd..ctor (System.String placementId) (at Assets/AudienceNetwork/Library/InterstitialAd.cs:103)
    InterstitialAdTest.LoadInterstitial () (at Assets/AudienceNetwork/Samples/InterstitialAdTest.cs:25)
    UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153)
    UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:634)
    UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:769)
    UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
    UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
    UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
    UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
    UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
    UnityEngine.EventSystems.EventSystem:Update()





    im facing this error message when im trying to add and ads for my game and i really cant understand where is the problem
     
  12. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,350
    Are you getting this while playing in the editor? you cannot instantiate any AndroidJavaObject or AndroidJavaClass objects in the editor, since they rely on running inside the Java VM (on an actual Android device).
     
    mekartikshah likes this.
  13. TravelLIN

    TravelLIN

    Joined:
    May 3, 2016
    Posts:
    1
    This helps run your build in editor but you will lose some functionality which can be tested only on device.
    Code (CSharp):
    1. #if UNITY_ANDROID && !UNITY_EDITOR
    2.     void Start()
    3.     {
    4.         AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    5.     }
    6. #endif
     
  14. raj244u

    raj244u

    Joined:
    Feb 28, 2018
    Posts:
    4
    i am also getting this error.Please help me out.

    _plugin = new AndroidJavaClass("com.unity3d.player.UnityPlayerActivity");
    _activityObject = _plugin.GetStatic<AndroidJavaObject>("currentActivity");
    Debug.Log(_activityObject);
    _activityObject.Call("StartPlugin");
     
  15. Muhammad_Taha

    Muhammad_Taha

    Joined:
    Jun 21, 2015
    Posts:
    9
    AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

    I got error on this line in Unity Editor. As explained earlier. I ignore it in Editor and now ads not showing on device. I got same error for Audience Network and Applovin Initialization. So, I need a proper way to solve this. Anybody knows?

    Regards.
     
  16. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    830
    JNI access is allowed only when running on device.
    Just add UNITY_ANDROID macro to avoid the code running when on editor.
     
  17. MontanaAnton

    MontanaAnton

    Joined:
    Feb 16, 2014
    Posts:
    5
    Enable Android JNI in "Package Manager" helped me

    Screenshot_78.png
     
unityunity