Validate function preconditions in android

The best practices here would be very similar to those used elsewhere in the Java world:

1. The first lines of a method are usually devoted to checking the validity of method arguments. The method should fail as quickly as possible in the event of an error.

When validating an argument, an Exception is thrown if the test fails. It's often one of these unchecked exceptions that are thrown:

  • IllegalArgumentException
  • NullPointerException
  • IllegalStateException

These are all derived from RuntimeException.

2. If every object parameter of every method in a class needs to be non-null in order to avoid throwing NullPointerException, then it's acceptable to state this once in the general class javadoc, instead of repeating it for each method.

References:

Preconditions, Postconditions, and Class Invariants.

EDIT:

To answer your question about "view specific for errors": while it is certainly possible to do that, the idea is that an Exception indicates the presence of programming errors in the code. Therefore, apps should be allowed to crash so that the user can report the error, and the developer thereby gets the error logs from the app's Play Store account. This way he can correct the sources of those errors. The process should continue till, hypothetically, the app is completely free of errors.