Friday, January 6, 2012

Android: Frequent mistakes/bugs

(Use Eclipse)
1. When you setOnClickListener on a button, Eclipse requests you change it to other method...
SOLUTION: import android.view.View.OnClickListener;

2. When you write only a service. Installed it into VD and you don't see its start icon. Although you can see that service in "Setting> App Manager> Running Service"
SOLUTION: Create an activity.

3. You edited some file but Eclipse keeps warning you have errors.  
SOLUTION: clean you project : Menu >Project >Clean

4. Some activities or services not run?
SOLUTION: Add them to AndroidManifest

5. You logged something but don't see them?
SOLUTION: Choose device which using by logcat.
Menu>Window>ShowView>Devices

6.You use Toast  but it not show on your screen
SOLUTION: did you forget .show() method?


7. Eclipse: Can't bind to local 8600 for debugger.
SOLUTION: In addition to adding "127.0.0.1 localhost" to your hosts file, make the following changes in Eclipse.
Under Window -> Preferences -> Android -> DDMS:
  • Set Base local debugger port to "8601"
  • Check the box that says "Use ADBHOST" and the value should be "127.0.0.1"
Then, call adb kill-server followed by adb start-server  
After done, if it still not work. Restart your computer

8. Bug when you want to change your UI
SOLUTION: Use  View.invalidate();

9.Error : NoSuchFieldError R$id...
SOLUTION: change that xml layout


10. Connect adb with you device  on Linux. Enter adb devices and get "Unknow device"
SOLUTION: sudo adb kill-server, sudo adb start-server, sudo adb service

11. Eclipse stops responding when changing to DDMS perspective

SOLUTION: The problem was that AVD hanged somewhere at 27% and then I couldn't do really anything with Eclipse. The problem is somewhere in the AVD and re-installing the AVD didn't help me.
Instead this seems to work every time:
  • Start virtual device from Eclipse's Window -> AVD menu
  • Then right click the project and "Debug as -> Android Application"
Here you never need to close the virtual device and it's also a lot faster to use it this way.

12. You are working with dialog and some view inside it not work

SOLUTION: make sure that you are work with approriate views. Maybe some findViewById() should be yourDialog.findViewById();

13. JUnit test
Logcat: Test run failed: Test run incomplete. Expected 1 tests, received 0

SOLUTION: you were using the wrong constructor. Eclipse auto generate the code and it created this:

public LoginTest(String pkg, Class<Login> activityClass) {
    super(pkg, Login.class);
}

change it to this:

public LoginTest() {
    super("pkg_name", Login.class);
}

note that all your test function must be start with test and its modifier is public
Ex:
public void testPreconditions() {
        assertNotNull(mView);
    }

14. Eclipse hangs on loading workbench (after .adt...)
SOLUTION:
remove .snap file from <<workspace_dir>>/.metadata/.plugins/org.eclipse.core.resources/ 
then run eclipse -clean

15. Create hotkey for commit current file with Subclipse
SOLUTION:
In order to get the shortcuts to work, you must go to Window > Customize Perspective, then under the Command Groups Availability tab, check the SVN option. It may require a restart of Eclipse, but the Subclipse keyboard shortcuts will now work.

Then create a key binding for 'Commit'
(under Preferences... General->Keys, search for Commit). Then you just need to click on the row SVN  and hit a key combination => Ok

16. Error on gen folder
SOLUTION:
check your res files, all filename must start with alphabet, not number.

17. Can't install your app on real device but emulator
SOLUTION:
Check your manifest, is there any duplicate activity?

No comments: