Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by lukiluk · Dec 14, 2012 at 03:09 PM · androidinvalidcastexception

How to get static int variables from Java?

For example, when I try to get Toast.LENGTH_LONG variable, using:

 using (AndroidJavaClass toastClass = new AndroidJavaClass("android.widget.Toast")) {
     int length = toastClass.GetStatic<int>("LENGTH_LONG");
 }

I get an error: InvalidCastException. Funnily, there is no InvalidCastException when using uint instead of int, but then I get a zero, instead of correct LENGTH_LONG value (which is 1). Everything works ok if I try to get a non-static int variable from Java, or when using a method (even a static one) to retrieve an int value, but GetStatic just doesn't work.

Comment
Add comment · Show 17
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 Eric5h5 · Dec 15, 2012 at 04:13 AM 2
Share

@Fattie: I thought we'd been over this a while ago...static has its place; telling people to never use it isn't helpful. I suspect that lukiluk is well past the point of needing general help with variable types.

avatar image Eric5h5 · Dec 15, 2012 at 03:56 PM 1
Share

@Fattie: I use static, Unity uses static. What do you think functions like Vector3.Lerp and hundreds of others are?

avatar image whydoidoit · Dec 15, 2012 at 04:06 PM 3
Share

I think static is misused by new programmers frequently, but is clearly a very important part of any language. Using statics as a way of communicating between classes compiles but ends up with the "one thing gets hit, they all get hit" problem.

I know I couldn't do without static methods, properties and fields.

This question is about getting something out of a Droid Java program though - so probably a bit more related to the conversion of variable types between the two languages...

avatar image Statement · Dec 15, 2012 at 04:47 PM 2
Share

@lukiluk I guess we can better help you if you post the definition of that static var so we can confirm if the type is wrong? The long route around if AndroidJavaClass can't do it for any unknown reason, you should be able to use AndroidJNI but this is way more verbose.

avatar image whydoidoit · Dec 15, 2012 at 06:03 PM 1
Share

Wo'there - this is getting a bit flamy - can we leave that alone please. This thread is now getting spammed and that is very unfair to the OP.

@Fattie is entitled to his opinions and with 11k karma points he has clearly helped a lot of people solve a lot of problems - many times involving a lot of hard work on his behalf. He also has a unique sense of humour!!! ;)

We don't agree about statics (or coroutines :) - but there you go. If you all don't $$anonymous$$d can we please just stop this debate unless it is technical and relevant to the question?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by zombiezen · Feb 05, 2013 at 09:26 PM

I ran into the same problem when trying to get the android.os.Build.VERSION.SDK_INT value. Here's what I did:

 private static function GetSDKLevel() : int {
     var clazz = AndroidJNI.FindClass("android.os.Build$VERSION");
     var fieldID = AndroidJNI.GetStaticFieldID(clazz, "SDK_INT", "I");
     var sdkLevel = AndroidJNI.GetStaticIntField(clazz, fieldID);
     return sdkLevel;
 }

Storing it as a variable is necessary because it comes back as a long, not an int, and there's not a convenient way to cast otherwise.

I have no idea why using the AndroidJNIClass doesn't work as expected.

Comment
Add comment · Show 2 · 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 petey · May 01, 2014 at 09:14 AM 0
Share

Thanks for posting this zombiezen!

avatar image RafaelMariano · May 01, 2014 at 01:03 PM 1
Share

Resolved??

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.

Follow this Question

Answers Answers and Comments

18 People are following this question.

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

Related Questions

Parsing Error, code in android for a button 2 Answers

Divide resolution and stretch to screen/window/canvas 1 Answer

Android aspect ratio for ui 0 Answers

Detect Gyroscope's rotation around a single axis? Like a car game. 0 Answers

Add arrays to a big array? 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