- Inflate Exception
- Surface.OutOfResourceException
- SurfaceHolder.BadSurfaceTypeException
- WindowManager.BadTokenException
In Android development, exceptions are typically used to handle errors or unexpected situations that may occur during the execution of a program. Here are some common exceptions that developers may encounter while working with Android:
- NullPointerException (NPE):
- Occurs when trying to access an object or invoke a method on an object that is
null
.
- Occurs when trying to access an object or invoke a method on an object that is
java
String str = null;
int length = str.length(); // This will throw a NullPointerException
- IllegalStateException:
- Thrown when an operation is performed that is not allowed in the current state of the application.
java
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.start(); // IllegalStateException if called without proper initialization
- IllegalArgumentException:
- Thrown when a method receives an argument of an illegal type or value.
java
Intent intent = new Intent(this, SomeActivity.class);
intent.putExtra("key", new Object()); // IllegalArgumentException for using an unsupported data type
- IndexOutOfBoundsException:
- Raised when trying to access an array, list, or string with an invalid index.
java
int[] array = {1, 2, 3};
int value = array[5]; // IndexOutOfBoundsException
- NetworkOnMainThreadException:
- Thrown when network operations are performed on the main thread.
java
// This will throw NetworkOnMainThreadException
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- SQLiteException:
- Commonly encountered when working with SQLite databases on Android.
java
// Example of SQLiteException
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM non_existent_table", null);
It’s important for Android developers to handle these exceptions appropriately to ensure robust and error-free applications. This can be done using try-catch blocks or other error-handling mechanisms.