• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jasperjasper · Aug 29, 2012 at 10:40 AM · jni

JNI problems in unity

Hi everyone.

I have encountered some JNI problem which happens on some Android device using ICS (Android 4.0.4). Below is the CatLog

08-29 09:33:56.457: E/Unity(18890): Initializng Facebook!!! 08-29 09:33:56.457: E/Unity(18890):
08-29 09:33:56.457: E/Unity(18890): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43) 08-29 09:33:56.457: I/Unity(18890): CallStatic("instance") = ()Ljava/lang/Object; 08-29 09:33:56.457: I/Unity(18890):
08-29 09:33:56.457: I/Unity(18890): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43) 08-29 09:33:56.457: I/Unity(18890): method id not cached; doing lookup (static) 08-29 09:33:56.457: I/Unity(18890):
08-29 09:33:56.457: I/Unity(18890): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43) 08-29 09:33:56.465: E/dalvikvm(18890): JNI ERROR (app bug): accessed stale weak global reference 0x13 (index 4 in a table of size 0)

May I ask if any one has encountered similar situation and know what is going on? Thank you

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mike McShaffry · Oct 02, 2012 at 10:44 PM

It is likely that the Java object you are trying to access has gone out of scope, or has a return value other than void.

So, when you make the call to CallStatic, you should probably quality the return value as an AndroidJavaObject.

Take a look at the C# script below:

 using (AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer") )
 {
    using (AndroidJavaObject activity = unityClass.GetStatic<AndroidJavaObject>("currentActivity") )
    {    
       activity.Call("startSensorService");            
    }
 }


You've probably seen this type of code in plugin examples. In the 2nd using statement, it calls a static member function of UnityPlayer to get the current activity object. If I called unityClass.GetStatic("currentActivity") without the qualifier, I'd get an error saying "can't find method id".

Also, if for some reason the return value from the Java code were null, a different kind of error would happen, which is also pretty common, the "JNI: Init'd AndroidJavaObject with null ptr" error.

The best to to avoid that error is to go into the Java code and make sure that these functions return something, or if they can't create another method like "hasInstance" or some such that can make sure the value you are about to return is a valid one.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unable to call method via AndroidJNI.CallStaticVoidMethod 0 Answers

Call non-static method (from subclass) in Java from Unity 1 Answer

How to call an android notification plugin if it's not the main activity? 1 Answer

How to add/edit onActivityResult for the activity created by currentActivity 1 Answer

Most consistent way to handle Timezones (plus JNI help) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges