Thursday, 26 June 2014

Replaced ADT Eclipse and got errors such as "The type java.lang.Object cannot be resolved"


I've installed the Juno version of the Eclipse-based ADT from the Google Android developers site in order to replace my outdated ADT. Now all of my existing android eclipse projects from my previous workspace have many errors such as "The type java.lang.Object cannot be resolved." and will not compile in the newly-installed ADT.

Steps to try:

The following steps fixed the issue on the author's system so they might be worth trying, (however it's not guaranteed that they will work):

  • Add any add-ons that may have been on your previous ADT build:
  • For each project, make sure that each has a valid Android target and Java system library
    • Right click on the project folder and click "properties"
    • Click on "Android" from the list on the left
    • Check an android version in "Project Build Target"
    • Click on "Java Build Path"
    • Click the "Add Library..." button on the right
    • Select "JRE System Library"
    • Choose an appropriate JRE and press "OK"

Explanation of the above steps:

The errors such as "The type java.lang.Object cannot be resolved" are due to the new ADT being unable to find the system's java library (or the library that is installed is currently invalid.) Pointing your project to the appropriate Java JDK should fix this problem. However, this doesn't fix everything.

The next errors regarding android.* and related means that the old ADT pointed to an Android target that likely no longer exists after the upgrade. Pointing the project to the new Android target should fix the problem (or installing the old versions should also do the trick.)

Hopefully this helped, though it definitely won't be a complete solution for everyone out there. Best of luck to you!

Other notes:

Note that the fix above worked for an upgrade to ADT version 23 using Eclipse Juno under OS X 10.9.3. Your mileage will most likely vary with different versions. The above steps were only listed in case someone out there finds them helpful.

Tuesday, 24 June 2014

Android CalledFromWrongThreadException


When I try to run functions such as myView.setText(myText), I get something similar to the following error: CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.


Use Activity.runOnUiThread()

For instance, within your MainActivity (or whatever your activity class is named):

public void myFunctionCalledByAnotherThread() {
  // ... stuff ...

  runOnUiThread(new Runnable() {
    public void run() {
      // ... code to modify ui such as View.setText() ...

  // ... other stuff ...

If you're outside of the Activity, you can also try getActivity():

getActivity().runOnUiThread( ... )


This error is caused by a thread other than the original thread that created the UI element/View, attempting to modify that view. An example of something that will cause this error is placing a setText() call into the run() function of a java TimerTask.