Sunday 27 April 2014

Common errors

Why should you know these?

Everyone wants to be an expert in Android development, and to be the expert in any development language you should know what kind of errors could arise and what are the reasons of errors/exceptions to solve them quickly.Also best developer is not that who has 3 to 5 years experience, but the best is that who knows what will happen if I use my logic in this way or that way, and will my logic work perfectly or it will arise some errors/exceptions.And this is the difference between Junior and Senior.Seniors predict the occurrence of errors/exceptions and avoid them using different approaches to develop applications rapidly.

1. Null Pointer Exception:

  • When we use an uninitialized variable or object we are creating.
  • When we use some layout view that is not in xml what we set in context.
  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throw-able value. 

OR

A NULL pointer is one that points to nowhere. When you dereference a pointer "p", you say "give me the data at the location stored in "p". When p is a null pointer, the location stored in "p" is "nowhere", you're saying "give me the data at the location 'nowhere'". Obviously it can't do this, so it throws a NULL pointer exception.

2. Class Cast Exception:

  • Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.
  • Casting to the wrong type on findViewById() when getting references to the UI widgets
  • when a program attempts to cast an object to a type with which it is not compatible. (e.g. when we try to use a linear layout which is declared as a relative layout in xml layout and vice versa).
  • Cast the String "1" to an int=no problem but,Cast the String "abc" to an int=raises a ClassCastException

3. Stack Over flow Exception:

  • It can occur in correctly written (but deeply recursive) programs. (Java and android)
  • when a program becomes infinitely recursive.
  • we create layout (deep and complex) that exceeds that stack of platform or virtual machine . recursive or too much of layout will create Stack overflow exception in Android also 
    Too many inner layouts could be the reason.

4. Activity Not Found Exception:

  • The activity is not declared in manifest or Forgetting to register new activities in the manifest.
  • when you call activity and it is not present in package.

5. Android Security Exception:

  • Reading data from storage and doesn't add permissions in Manifest.
  • You need to declare all permissions in the application Manifest that your application uses (Internet, access to contact, GPS, WIFI state,write to SDCard, etc).

6. Out Of Memory Exception OR Monster Exception:

  • bitmap size exceeds VM budget.
  • when a request for memory is made that can not be satisfied using the available platform resources . mainly using high resolution images, bitmap, gallery etc.

7. Application Not Responding (ANR):

  • Mainly comes when you are making network function,or some long process.this will block UI Thread so user can not do any work. to avoid ANR.

8. Original Thread That Created A View Hierarchy Can Touch Its Views:

  • You must have tried to update a view content from another thread than the UI thread. So either create a handler in your UI thread and post your Runnable to this handler OR use the method runOnUIThread to run the lines of code that are doing the update.
9. R.layout.main Cannot Be Found/Missing:
  • R refers to the resource file. In your source code check if you did not import android.R. An android.R import will prevent Eclipse from finding your R file. Also you can check the spelling of the xml layout file name (Here in ‘R.layout.main’, main is the layout file name) , make sure that the xml file name should not contain any space.
10. INSTALL_FAILED_INSUFFICIENT_STORAGE:
  • when installing an application on emulator and emulator doesn't have storage to install application.By default Android virtual device(AVD) provides only 64M for the memory space for Android applications. You can clean your installed application by re-starting the emulator and selecting the Wipe user data flag may solve this.Also you can set the data partition size by launching AVD Manager, then if you press edit on the AVD, you can set the SD Card size.
So I mentioned some common errors we face while developing Android Applications that generally causes force close, so I hope that you will take care of these exceptions while developing Android applications.So try to use try - catch block in all places of program. Don't leave your catch block empty as this can hide errors:

 try{
    // try something
  } catch (Exception e) {
      Log.e("TAG", "Exception in try catch", e);
      return false;
  }
  return true;

I think many of us have faced these exceptions,want to listen from you,have you ever faced these exceptions?