• 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 darxval · Feb 28, 2012 at 01:15 AM · androidplugin

Plugin Current Activity

Hey all,

I am working on some plugins starting with Android, mainly I am creating a plugin for Flurry. I am currently overriding the activity with my own activity class. and onStart() I am having it call a C# script w$$anonymous$$ch calls the Java function on the activity and start's the session. But the issue is on the Call from Unity to the activity.

if i do the following, where flurry plugin is the overriding activity class.

 AndroidJavaClass jc = new AndroidJavaClass("net.company.FlurryPlugin");
 AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
 jo.Call<string>("startFlurrySesson", new object[] {APIKEY});

Error

 E/Unity   (26512): getFieldID("currentActivity", "Ljava/lang/Object;") FAILED!
 I/Unity   (26512): JNI: Unable to find field id for 'currentActivity' (static)
 I/Unity   (26512):
 I/Unity   (26512): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
 I/Unity   (26512):
 I/Unity   (26512): JNI: Init'd AndroidJavaObject with null ptr!
 I/Unity   (26512):
 I/Unity   (26512): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
 I/Unity   (26512):
 I/Unity   (26512): JNI: Unable to find method id for 'startFlurrySesson'
 I/Unity   (26512):
 I/Unity   (26512): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
 I/Unity   (26512):


But if i use

 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
 AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
 jo.Call<string>("startFlurrySesson", new object[] {APIKEY});

I only get the following error, w$$anonymous$$ch makes sense because t$$anonymous$$s method doesnt exist in the base class.

  I/Unity   (26120):
     I/Unity   (26287): JNI: Unable to find method id for 'startFlurrySesson'
     I/Unity   (26287):
     I/Unity   (26287): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
     I/Unity   (26287):


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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by darxval · Feb 29, 2012 at 12:07 AM

Alright so I figured it out. And it was just so obvious after I reread the documentation.

My Java function is Void and not String and so the method doesn't exist, what is awesome is that the JavaClass that is grabbed must end up with your new class as you call methods that you make with the new extended class.

AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic("currentActivity"); jo.Call("startFlurrySesson", new object[] {APIKEY});

Comment
Add comment · Show 1 · 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
avatar image appearance · Sep 11, 2012 at 11:34 AM 0
Share

Hi, Can you show here how you have written "startFlurrySession()" function in android as you are passing parameter(s) to that function.

Also what is the type of APIKEY in 'new object[]{APIKEY}' as parameter?

Thank you.

avatar image
1

Answer by netlander · Jul 12, 2013 at 09:58 AM

@appearance.

As usual you create the boilerplate code as follows:

         if (Application.platform == RuntimePlatform.Android)
         {
             jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
             currentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
             flurryHelper = currentActivity.Get<AndroidJavaObject>("flurryHelper");
         }

Then you can call methods on the flurryHelper class like so:

         AndroidJavaObject flurryDataList = flurryHelper.Call<AndroidJavaObject>("getDataList");
         int dataTableSize = flurryDataList.Call<int>("size");
 

Note that t$$anonymous$$s example is not Flurry specific but a general way of writing code that passes parameters and get return values back from c# to Java.

In the last code snippet, the first line is calling a method getDataList() and returning a generic 'AndroidJavaObject'. No need to cast it or otherwise convert the object if you just want to call methods on it.

The final line is calling a method on the returned object (above), t$$anonymous$$s time the return value is an int.

Finally, if you want to pass in params to the Java methods you do somet$$anonymous$$ng like t$$anonymous$$s:

 flurryData = flurryList.Call<AndroidJavaObject>("get", index);

In t$$anonymous$$s case index is an int but we can pass in any type the receiving method accepts. In the code you were enquiring about, object[]{APIKEY} is an array of generic objects, t$$anonymous$$s will be converted into strings on the other side.

Hope t$$anonymous$$s helps. Netlander

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
avatar image
-2

Answer by ranayahya87 · Feb 12, 2014 at 05:56 AM

Hi, i am implementing InAppPurchase in unity for android. i downlkoad sample project from t$$anonymous$$s website. http://ge.tt/51dEEEO/v/0 when i run t$$anonymous$$s sample project on unity project it gives error "JNI Init'd AndroidJavaClass with null pointer " and when i run it on device it crash some times and one times it run fine. when same code i implement in my own game project it always crash on device. please help me. Thanks in advance. Rana.Suave

Comment
Add comment · Show 1 · 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
avatar image cowlinator · Mar 26, 2015 at 01:44 AM 0
Share

If you have a question, please post it as a question, not as an answer.

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

7 People are following this question.

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

Related Questions

Plugin Android that doesn't work anymore in Unity 4.3 0 Answers

How to add i-ad for android unity game? 0 Answers

Android native plugin question 1 Answer

How to use Android Api in Unity 1 Answer

How to use android Renderscript v8 with Unity? 0 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