• 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 waqa1988 · Sep 30, 2011 at 07:46 AM · androidjavaobject

AndroidJavaClass and AndroidJavaObject question

I have a class named MyClass,it inherited from UnityPlayerActivity,it also has a method

 public int getValue(){
 return 20;
 }

I want to use AndroidJavaObject to get this value of this method. my code as following:

 using(AndroidJavaObject obj=new AndroidJavaObject("com.mxr.test.MyClass")){    
 value=obj.Call<int>("getValue");    
 }

but,when I run it,I can't get the value, any body can help me? thanks a lot

Comment
Add comment · Show 2
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 alvaro-em · Apr 04, 2012 at 06:38 PM 0
Share

Did you managed to solve this? I assume that it must be a really noob question because it has no answer, but I am right now trying to understand all this and I am facing the same problem. Could you give me a hand?

Thx in advance!

avatar image tribio · Oct 05, 2013 at 11:40 AM 0
Share

Yes! I got the same problem! Did you sort it out?

thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by liortal · May 26, 2014 at 06:02 PM

When you inherit from UnityPlayerActivity, I suppose you also use this as the main activity for the game (also involves creating a new AndroidManifest.xml that declares that your activity will be used).

In this case, when you launch your game on Android, the OS will create an instance of the Activity class for you and call its relevant callbacks (e.g: onCreate, onResume, etc).

In your code, you attempt to create an AndroidJavaObject with the name of the class you given your new Activity.

However, according to the AndroidJavaObject constructor documentation:

Construct an AndroidJavaObject based on the name of the class.

This essentially means locate the class type, allocate an object and run the specified constructor.

This means you are attempting to create another instance of your class. IIRC, you cannot instantiate an Activity class yourself as it has no public constructor (it is created for you by the Android OS).

Therefore, what happens in your case i guess is that Unity "swallows" some error and doesn't do anything.

What you'd like to do is keep some static field that stores a reference to the created activity object, and then use that to call methods on it.

See this answer for more details: http://answers.unity3d.com/questions/655299/androidjavaclasscall-returns-empty-string.html

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

how to use database in unity for developing android games? 0 Answers

Unity to Android: passing CameraCharacteristics.Key values from Unity to Android cameraCharacteristics.get? 1 Answer

Using AndroidJavaObject properly 1 Answer

Detecting if an app is installed and still installed on Android device 0 Answers

Android Plugin in Unity - No Return Value 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