Thursday, 26 June 2014

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

Problem:

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: http://developer.android.com/sdk/installing/adding-packages.html
  • 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

Problem:

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.

Solution:

Use Activity.runOnUiThread()

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


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

  runOnUiThread(new Runnable() {
    @Override
    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( ... )

Explanation:

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.

References:

Tuesday, 13 May 2014

'MySql server has gone away' with phpmyadmin using MAMP 2.0.5

Problem:

In phpmyadmin under MAMP 2.0.5, I get the error 'MySql server has gone away' when trying to import a large .sql file.

Solution:

There are a few things that can cause this issue, however one common one is the MySQL max packet size being too small. By default, it's set to about 1MB.

To fix this under MAMP 2.0.5, you can add a custom configuration setting to increase the max packet size by using the following instructions:

  • Stop MySQL Server (or stop all MAMP servers)
  • In /Applications/MAMP/conf/ create a my.cnf file if it does not exist
  • Add the following to the my.cnf file:
    [mysqld]
    max_allowed_packet = 64M
    
  • Save the my.cnf file
  • Restart MAMP servers

If this did not quite solve the problem, you can check out this Stack Overflow post for additional my.cnf configurations to try.

References:

Wednesday, 26 February 2014

In XCode 5, turn on 80-column line guide

Problem:

I am using XCode 5 and the coding style guide that I am using requires avoiding lines going past the 80-column mark, if possible.

How do I turn on the 80-column guide?

Solution:

  1. Go to the XCode Preferences (in the menu: XCode -> Preferences...)
  2. Go to the Text Editing tab
  3. Check "Page guide at column: "
  4. Change the number to 80, or to whatever column your required style guide needs

Friday, 21 February 2014

Toggle Doxygen in Eclipse Kepler CDT C/C++ Editor

Problem:

I'm working on C/C++ in Eclipse Kepler and I would like to toggle the IDE editor's Doxygen comment helpers as they appear to be turned off by default. How do I do this?

Solution:

To toggle the "automatically generate Doxygen comment" feature in Eclipse, go to:

Preferences ... -> C/C++ -> Editor

Then look for the panel labeled Documentation tool comments and change Workspace default to "None" or "Doxygen", depending on your preference.

This is illustrated in the following image:

Notes:

If you are unsure of what Doxygen is, check out this quick explanation from Wikipedia (as of Feb 21, 2014):

Doxygen is a documentation generator, a tool for writing software reference documentation. The documentation is written within code, and is thus relatively easy to keep up to date. Doxygen can cross reference documentation and code, so that the reader of a document can easily refer to the actual code.
Article link: http://en.wikipedia.org/wiki/Doxygen. It's a fairly useful tool and you should consider using it if you don't already have a consistent style for writing comments in C++ code.

References:

Monday, 3 February 2014

How to detect displays in OS X 10.9 Mavericks

Problem:

How do I detect displays in OS X Mavericks? The Systems Preferences option to detect displays seems to be missing.

Solution:

While in the "Displays" Systems Preferences panel, press the "option" key and the "Detect Displays" button will appear when using OS X Mavericks.

Image below: OS X 10.9 Displays panel with the "Detect Display" button missing by default

Image below: OS X 10.9 Displays panel after pressing the "Option" key

Notes:

This was tested to work in OS X 10.9 and 10.9.1. Your mileage may vary in other versions.

References:

Monday, 13 January 2014

Check if music is already playing on Android from within an Activity

Problem:

How do I check if music is already playing on an Android device from within an Activity?

Solution:

AudioManager aMan = (AudioManager)this.getSystemService(
                                         Context.AUDIO_SERVICE);
if(aMan.isMusicActive()) {
  // do stuff here if music is playing
}

Notes:

This check can be useful if your app needs to play music. This is because there are instances where attempting to play music using MediaPlayer while music is already playing will corrupt an audio stream on an Android device.

References:

Tuesday, 7 January 2014

Disable Mac start-up chime in OS X 10.9 Mavericks ("Band-Aid" workaround only, for now)

Problem:

I would like to disable the start-up chime my OS X Mavericks-based Mac makes so that restarting the computer will not be so loud.

Notes:

  • The solutions below were tested on OS X 10.9 Mavericks and may work on other versions, but there is no guarantee.
  • This how-to was written as a reminder for myself in case this needs to be replicated or undone later. It is not a full solution and still a bit advanced for the average OS X user to follow, which makes it unacceptable to post as a "solution" (in my opinion).
  • You're welcomed to follow the solutions below, but use the more permanent one at your own risk.

Solution 1 (temporary):

Turn the volume of your computer down before shutdown or restart. The start-up chime should match your system's volume.

Note that muting seems to have mixed reported results in forums and other online posts, where some systems will chime at the same volume as the system volume, even when muted (resulting in lots of surprise chimes), yet others report that muting works.

Solution 2 (advanced workaround, but a bit more permanent):

This solution goes through the steps to write a script that turns the system volume to 0, and rigs it to run upon the user selecting to shutdown or restart the machine via the apple menu.

Warnings:

  • Make sure you are comfortable with Terminal, shell scripts, file permissions, and reading manual pages (if you get stuck). If you are not comfortable using Terminal (or are prone to making typos), you should probably not try this. Attempt at your own risk.
  • Every time you start up OS X, the volume will be set to 0 (which may not be a bad thing if you're already trying to avoid surprises by turning the chime sound off to begin with)

This is a modification of the solution found here, but modified in a way that should work on more OS X setups. The modified steps are posted below as a reminder in case the link ever goes down.

Steps:

  1. Run the Terminal app with an administrator account
  2. Create a script for muting (replacing "/path/to/" with a valid path -- if you're not sure what this means, do not proceed with the rest of the steps)
    sudo nano /path/to/volume-off.sh
    
  3. Write the volume-off script (or copy and paste the following into it):
    #!/bin/bash
    
    osascript -e "set Volume 0"
    
  4. Make file executable with following command (replacing "/path/to/" with where the script is saved):
    sudo chmod u+x /path/to/volume-off.sh
    
  5. Check that we can hook this to the OS X logout (see if any hooks exist - only one or none can exist, not more). In Terminal, use the command:
    sudo defaults read com.apple.loginwindow LogoutHook
    

    You should end up with something similar to "The domain/default pair of (com.apple.loginwindow, LogoutHook) does not exist".

  6. Add hook to run script at logout, replacing "/path/to/" with where the script is saved:
    sudo defaults write com.apple.loginwindow LogoutHook /path/to/volume-off.sh
    

To Undo Solution 2:

  1. Check that the logout hook exists in Terminal with the following command:
    sudo defaults read com.apple.loginwindow LogoutHook
    

    You should end up with something similar to "/path/to/volume-off.sh" with "/path/to/" being where your script is saved.

  2. Delete the logout hook in Terminal with the following command:
    sudo defaults delete com.apple.loginwindow LogoutHook
    

Notes on solution 2:

  • Just like in the original solution, the volume-off.sh script is also saved in /Library/Scripts/
  • Make sure the permissions of the script is set so that only the owner can write to the file, since the script is run as sudo (and would be an obvious security risk if it can be re-written by just anyone).
  • The script must be owned by root.
  • Again, this will turn the volume to 0 upon logout rather than simply mute. This is because many people online seem to report mixed success with muting.
  • The commands for setting both a login and a logout hook, as well as removing them are as follows:
    • sudo defaults write com.apple.loginwindow LoginHook /path/to/your-login-script.sh
    • sudo defaults write com.apple.loginwindow LogoutHook /path/to/your-logout-script.sh
    • sudo defaults delete com.apple.loginwindow LoginHook
    • sudo defaults delete com.apple.loginwindow LogoutHook
  • The commands for checking if a hook exists for login or logout are as follows:
    • sudo defaults read com.apple.loginwindow LoginHook
    • sudo defaults read com.apple.loginwindow LogoutHook

Very brief discussion on problems with other solutions found online:

The following will only make sense to anyone who has Googled how to disable the start-up Mac chime and read through the various proposed solutions. It briefly outlines why these workarounds weren't posted here. You can skip this section if you'd like.

nvram SystemAudioVolume method:

  1. too dangerous for most users (you can easily accidentally turn your computer into an expensive paperweight with the nvram command)
  2. many mixed results and uninformed posts online on why that solution used to work (read: everyone seems to be guessing. If you don't want to be one of those guessers, you can read up on nvram in the slightly out-of-date book "Mac OS X for Unix Geeks (Leopard)" in Google Books as that section appears to be viewable for free, at least as of January 2014)
  3. the solution doesn't work on all OS X and Mac combinations
  4. the solution does NOT work in Mavericks (tested) as the system will change the SystemAudioVolume value automatically

rc startup/shutdown script method:

  1. does not work in Mavericks as start-up has been moved to launchd instead (see Apple developer documentation on Launch Daemons and Agents
  2. any daemon-supporting mechanism may change in future versions of OS X, making this solution possibly unreliable (and worse, it will leave potentially unwanted files dangling deep within the system)
  3. this solution is way too complicated to post for the average user

existing software:

  1. many don't work on all versions of OS X (mixed results reported in forums, etc.)
  2. all software Googled do not describe exactly what system-wide changes are being made, making it hard to determine how permanent they are, as well as how well do they handle OS upgrades
  3. Mavericks seems to have broken a lot of the existing software, so it's hard to recommend a good one

the solution 2 in this blog post:

  1. it's not possible to do if you are not the system administrator
  2. it's not possible to do if you or your system administrator has already hooked something to com.apple.loginwindow LogoutHook
  3. it's possible that it won't work if you shut down your computer using a different mechanism than the usual Apple UI ways
  4. it's still inaccessible to most users
  5. if permissions are not set correctly, you could potentially create a security hole

an official Apple "turn off start-up sound" preferences item:

  1. does not exist/was eliminated
  2. if you're from Apple, perhaps you could help out with this? =)

References: