Saturday, September 15, 2012

NullPointerException when itemizedOverlay doesn't contain any Overlay item

Yesterday when I was working in Android, I got a strange exception. I am adding an ItemizedOverlay to google map. I am adding OverlayItem to the ItemizedOverlay when one condition is true. But sometimes, this condition is false and not adding any OverlayItem to the ItemizedOverlay. But still adding the ItemizedOverlay to google maps. At this time, I am getting null pointer exception. First I didn't get what is going wrong. Then after some googling, I came to know we can't add ItemizedOverlay to google map when there is no OverlayItem in the Overlay. So be careful when adding empty ItemizedOverlay to google maps. I was working in API level 10. I am not sure whether this problem is in future versions.

Tuesday, June 26, 2012

Show the application only in portrait mode in Android

To show the app only in portrait, in the application tag of your manifest file of the application you need to add one more attribute. The attribute to show the app in portrait is,
Or if you don't want the entire application in portrait mode, only some screens(Activity) to be in portrait mode, then put the same tag,
to whichever activity you want in portrait mode. The activity tag will look like,
In activity class also, you can restrict to portrait mode by adding 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); in the onCreate() before calling setContentview()

<uses-feature> tag and required attribute in Android

In this tutorial I am going to explain <uses-feature> tag with respect to camera. When writing an Camera application, first in manifest we need to add the permission as
<uses-permission android:name="android.permission.CAMERA"/>
Also, we can give additional requirements in manifest file like, only those devices which have Camera can install our Camera app by
<uses-feature android:name="android.hardware.CAMERA" required="true"/>
If required attribute is false, then if CAMERA is not there in the device, then also app will install but once we mention required attribute is true in uses-feature then if CAMERA is not in the device then we can't install the app in the device.

Hide Title bar in Android

A simple but useful code snippet to hide title bar in Android. The code snippet is


The important thing is, you need to put this code in onCreate() of the activity class. That also before setContentView(). If we put the requestWindowFeature() after setContentView() then we will get an exception. The exception will look like,

E/AndroidRuntime(  277): FATAL EXCEPTION: main
E/AndroidRuntime(  277): java.lang.RuntimeException: Unable to start activity ComponentInfo 
android.util.AndroidRuntimeException: requestFeature() must be called before adding content

So the code will be like,
public void onCreate(Bundle savedInstanceState) {

Thursday, June 14, 2012

Referring Resources in an Application through URI

Normally we refer resources in an application using resource ID. There is an alternative way to refer resources in Android. That is through Uri. This will be useful when we want to load an image in webview using loadUrl() for zooming.

The format is,


eg: Uri.parse("android.resource://com.smartandroidians.sample/"+R.raw.zoom_image);