Buggy code:
public class RhybuddHome extends Activity { private SharedPreferences settings = null ; private Handler HomeHandler = null ; private Handler runnablesHandler = null ; @ SuppressWarnings ( "unused" ) private Runnable updateEvents = null ; @ SuppressWarnings ( "unused" ) private Runnable updateDevices = null ; @ SuppressWarnings ( "unused" ) private Runnable updateDeviceDetails = null ; private Boolean OneOff = true ; @ Override public void onAttachedToWindow ( ) { } public void onCreate ( Bundle savedInstanceState ) { } @ Override public boolean onCreateOptionsMenu ( Menu menu ) { } @ Override public boolean onOptionsItemSelected ( MenuItem item ) { } private void ConfigureHandler ( ) { } private void ConfigureRunnable ( ) { updateDevices = new Runnable ( ) { public void run ( ) { HomeHandler . sendEmptyMessage ( 100 ) ; HomeHandler . sendEmptyMessage ( 1 ) ; Thread devicesRefreshThread = new Thread ( ) { public void run ( ) { ZenossAPIv2 API = null ; JSONObject DeviceObject = null ; SQLiteDatabase cacheDB = null ; try { API = new ZenossAPIv2 ( settings . getString ( "userName" , "" ) , settings . getString ( "passWord" , "" ) , settings . getString ( "URL" , "" ) ) ; if ( API != null ) { DeviceObject = API . GetDevices ( ) ; int DeviceCount = DeviceObject . getJSONObject ( "result" ) . getInt ( "totalCount" ) ; cacheDB = RhybuddHome . this . openOrCreateDatabase ( "rhybuddCache" , MODE_PRIVATE , null ) ; cacheDB . delete ( "devices" , null , null ) ; for ( int i = 0 ; i < DeviceCount ; i ++ ) { JSONObject CurrentDevice = null ; ContentValues values = new ContentValues ( 2 ) ; try { CurrentDevice = DeviceObject . getJSONObject ( "result" ) . getJSONArray ( "devices" ) . getJSONObject ( i ) ; values . put ( "productionState" , CurrentDevice . getString ( "productionState" ) ) ; values . put ( "ipAddress" , CurrentDevice . getInt ( "ipAddress" ) ) ; values . put ( "name" , CurrentDevice . getString ( "name" ) ) ; values . put ( "uid" , CurrentDevice . getString ( "uid" ) ) ; values . put ( "infoEvents" , CurrentDevice . getJSONObject ( "events" ) . getInt ( "info" ) ) ; values . put ( "debugEvents" , CurrentDevice . getJSONObject ( "events" ) . getInt ( "debug" ) ) ; values . put ( "warningEvents" , CurrentDevice . getJSONObject ( "events" ) . getInt ( "warning" ) ) ; values . put ( "errorEvents" , CurrentDevice . getJSONObject ( "events" ) . getInt ( "error" ) ) ; values . put ( "criticalEvents" , CurrentDevice . getJSONObject ( "events" ) . getInt ( "critical" ) ) ; cacheDB . insert ( "devices" , null , values ) ; } catch ( JSONException e ) { e . printStackTrace ( ) ; } } cacheDB . close ( ) ; } } catch ( ClientProtocolException e1 ) { e1 . printStackTrace ( ) ; } catch ( JSONException e1 ) { e1 . printStackTrace ( ) ; } catch ( IOException e1 ) { e1 . printStackTrace ( ) ; } catch ( Exception e1 ) { e1 . printStackTrace ( ) ; } finally { if ( ( cacheDB != null ) && ( cacheDB . isOpen ( ) ) ) cacheDB . close ( ) ; } HomeHandler . sendEmptyMessage ( 99 ) ; runnablesHandler . postDelayed ( this , 3604000 ) ; <START_BUG> if ( cacheDB . isOpen ( ) ) <END_BUG> cacheDB . close ( ) ; } } ; devicesRefreshThread . start ( ) ; } } ; updateEvents = new Runnable ( ) { public void run ( ) { HomeHandler . sendEmptyMessage ( 100 ) ; HomeHandler . sendEmptyMessage ( 0 ) ; Thread eventsRefreshThread = new Thread ( ) { public void run ( ) { HomeHandler . sendEmptyMessage ( 100 ) ; HomeHandler . sendEmptyMessage ( 0 ) ; JSONObject EventsObject = null ; JSONArray Events = null ; try { ZenossAPIv2 API = new ZenossAPIv2 ( settings . getString ( "userName" , "" ) , settings . getString ( "passWord" , "" ) , settings . getString ( "URL" , "" ) ) ; EventsObject = API . GetEvents ( settings . getBoolean ( "SeverityCritical" , true ) , settings . getBoolean ( "SeverityError" , true ) , settings . getBoolean ( "SeverityWarning" , true ) , settings . getBoolean ( "SeverityInfo" , false ) , settings . getBoolean ( "SeverityDebug" , false ) ) ; Events = EventsObject . getJSONObject ( "result" ) . getJSONArray ( "events" ) ; } catch ( Exception e ) { HomeHandler . sendEmptyMessage ( 0 ) ; } try { if ( EventsObject != null ) { int EventCount = EventsObject . getJSONObject ( "result" ) . getInt ( "totalCount" ) ; SQLiteDatabase cacheDB = RhybuddHome . this . openOrCreateDatabase ( "rhybuddCache" , MODE_PRIVATE , null ) ; cacheDB . delete ( "events" , null , null ) ; for ( int i = 0 ; i < EventCount ; i ++ ) { JSONObject CurrentEvent = null ; ContentValues values = new ContentValues ( 2 ) ; try { CurrentEvent = Events . getJSONObject ( i ) ; values . put ( "EVID" , CurrentEvent . getString ( "evid" ) ) ; values . put ( "device" , CurrentEvent . getJSONObject ( "device" ) . getString ( "text" ) ) ; values . put ( "summary" , CurrentEvent . getString ( "summary" ) ) ; values . put ( "eventState" , CurrentEvent . getString ( "eventState" ) ) ; values . put ( "severity" , CurrentEvent . getString ( "severity" ) ) ; cacheDB . insert ( "events" , null , values ) ; } catch ( JSONException e ) { } } cacheDB . close ( ) ; } }

Fix:
if ( ( cacheDB != null ) && ( cacheDB . isOpen ( ) ) )