Monday, March 26, 2012

Android Emulator won't connect to internet

Many a time I see this problem where the Android emulator simply refuses to connect to internet. There are 2 easy solutions to fix this problem.

1. The primary reason is that your machine might have both LAN and Wireless enabled and the internet connection is available only on Wireless. Since the emulator always tries LAN to for DNS settings it will be unable to connect. To fix this problem

  • Right click on your "My Network Places" and select properties

  • Select your LAN connection and disable it.

  • Restart your emulator


2. If the above solution doesn't work then try launching your emulator with the following command prompt.
emulator -avd <avd name> -verbose -dns-server 8.8.8.8

or if you are using eclipse to start the emulator then


    • Right click on your project and select "debug as"

    • Select "debug configuration" and select "target" tab

    • Enter the following text in the text field below "Additional Emulator Command Line Options"
      -dns-server 8.8.8.8




Emulator Configuration

  • The above steps applies to "Run Configuration" as well.

Tuesday, March 20, 2012

ProgressDialog with custom view

In one of my application I had to download data from server which took almost 10-15 seconds and I didn't want my users to stare at a progress dialog with just loading message.

I wanted to customize the default ProgressDialog to display interesting facts so that the user feels that they are waiting too long for the data.

My first step was to create my own layout and set the layout using the method setContentView method. But seems this method though available but cannot be used since it throws the following error
android.util.AndroidRuntimeException: requestFeature() must be called before adding content

So I tried to write my own dialog by sub classing Dialog but figured out that there is a better way by sub classing AlertDialog.

[java]
AlertDialog.Builder builder = new AlertDialog.Builder(context);
progressView = LayoutInflater.from(context).inflate(R.layout.progress_dyk, null);
builder.setView(progressView);
progress = builder.create();
progress.requestWindowFeature(Window.FEATURE_NO_TITLE);

progress.show();
[/java]

Eclipse: JVM Terminated. Exit code=-1

I have been using my eclipse instance for months and on the day of a crucial release it stopped working (May be just to prove Murphy's law). I had several instance of eclipse (I usually have different instance for J2EE, Android, Blackberry etc) and all the instance showed me exactly the same error.





[xml]
JVM terminated. Exit code=-1
-Dosgi.requiredJavaVersion=1.5
-Xms40m
-Xmx512m
-XX:MaxPermSize=512M
-Djava.class.path=D:eclipseAndroideclipseplugins/org.eclipse.equinox.launcher_1.0.201.R35x_v20090715.jar
-os win32
-ws win32
-arch x86
-showsplash
-launcher D:eclipseAndroideclipseeclipse.exe
-name Eclipse
--launcher.library D:eclipseAndroideclipseplugins/org.eclipse.equinox.launcher.win32.win32.x86_1.0.200.v20090519eclipse_1206.dll
-startup D:eclipseAndroideclipseplugins/org.eclipse.equinox.launcher_1.0.201.R35x_v20090715.jar
-product org.eclipse.epp.package.java.product org.eclipse.platform
-vm C:Javajre6binclientjvm.dll
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms40m
-Xmx512m
-XX:MaxPermSize=512M
-Djava.class.path=D:eclipseAndroideclipseplugins/org.eclipse.equinox.launcher_1.0.201.R35x_v20090715.jar
[/xml]


Tweaking all the memory size parameters in eclipse.ini but irrespective of whatever permutation and combination I tried it didn't budge.

I then removed all the contents of eclipse.ini and voila! it worked perfectly but then I thought these values are there for a reason and removing everything might cause some other issue. Hence I started putting back the values one by one and figured out that removing the following 2 lines in the ini file fixed the problem.
[java]
--launcher.XXMaxPermSize
512M
--launcher.XXMaxPermSize
512M
[/java]
After pin pointing the problem area I added these parameters but tried with different values and after trial and error found that value 128M worked perfectly. Now the final eclipse.ini looks like the one shown below.
[java]
-startup
plugins/org.eclipse.equinox.launcher_1.0.201.R35x_v20090715.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.0.200.v20090519
-product
org.eclipse.epp.package.java.product
-showsplash
--launcher.XXMaxPermSize
128M
--launcher.XXMaxPermSize
128M
org.eclipse.platform
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms40m
-Xmx512m
[/java]
I am still not sure what is the root cause of this issue. May be restarting the machine and reverting back the value to 512 M might do the trick.