• 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 davidschep · Feb 13, 2017 at 08:35 AM · c#androidjavabug-perhapsthreading

Can't start a Handler on the main thread

I'm working on a .jar api to read Google Fit data from Unity3D. The problem that I'm facing right now is that when I want to execute this block of code:

private void buildFitnessClient() { mClient = new GoogleApiClient.Builder(myContext).addApi(Fitness.HISTORY_API) .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE)) .addConnectionCallbacks(new ConnectionCallbacksListener()) .enableAutoManage(this, 0, new ConnectionFailedListener()).build(); }

I get the error: java.lang.IllegalStateException: Must be called from main thread of process

This error is due to the this reference in the last sentence of the line of code.

Now online I found that I could fix this by starting a Handler on the Main Looper, I tried this with this code:

new Handler(Looper.getMainLooper()).post( new Runnable() { @Override public void run() { try { buildFitnessClient(); } catch (Exception ex) { Log.i(TAG, ex.getMessage() + "\n"); } } });

For some reason my Unity app instantly crashes, usually I can get the error since I use a try catch statement. With this problem it instantly crashes and there is no log I can check.

When I create my Handler without the mainlooper, like:

new Handler()

or

new Handler(Looper.myLooper())

The code runs, but the java.lang.IllegalStateException error remains, and the Thread.currentThread() obviously remains the same.

The thread that the program runs on is a thread created by Unity.

Thanks in advance!

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 davidschep · Feb 13, 2017 at 11:04 AM 0
Share

the program crashes on 'this' in the enableAuto$$anonymous$$anage(this, 0, ....

avatar image davidschep · Feb 13, 2017 at 11:33 AM 0
Share

It seems like the thread starter works, it crashes on the method that builds the fitness client?

0 Replies

· Add your reply
  • Sort: 

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

8 People are following this question.

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

Related Questions

Getting String from Java Class on Android 0 Answers

Error building with custom java code [Solved] 1 Answer

How to write a java lib for unity as a plugin to make run another android app? 1 Answer

Integration of Java,C++,C# in Unity 0 Answers

java android plugin worker thread ends up in Unity mainthread 1 Answer


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