[SOLVED] A resource failed to call close in Android studio java

Question

This Content is from Stack Overflow. Question asked by marivic valdehueza

I know this is common to ask but , I wondered why I’ve receiving this error when I queries data from SQLite , I cannot viewed the exact error and when I check the logcat it says on the last line A resource failed to call close need help

 String s = "160310001-0111-00210";
        btn_search_hh.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT id,full_name FROM emv_database_monitoring WHERE hh_id="+s);
                while (cursor.moveToNext()) {
                    String status = cursor.getString(1);
                    Log.v(TAG,"why" +status);
                }


            }
        });

Solution

The answer from @guest works, but you can achieve the exact same thing without resorting to reflection using Strict Mode. Specifically, something like:

StrictMode.setVmPolicy(new VmPolicy.Builder()
                 .detectLeakedClosableObjects()
                 .penaltyLog()
                 .build());

In general, strict mode can do much more for you though (see link above to doc), and all you need to do for a default setup is:

StrictMode.enableDefaults();  # <-- This includes warning on leaked closeables

To enable strict mode “as soon as possible” you can add either of the above code options to the constructor of your application class, e.g.:

public class MyApp extends Application {

    public MyApp() {
        if(BuildConfig.DEBUG)
            StrictMode.enableDefaults();
    }

}

Note that in addition to just creating the class above, you need to tell Android that you have created a custom application class in the AndroidManifest.xml (so that an instance of it gets created when your application process starts, instead of Android creating the default Application class). You need to add/modify the android:name attribute of the <application> tag to point to the fully resolved package path of your custom application class (MyApp in this case):

<application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:name="com.example.app.MyApp"  <-- IMPORTANT PART: ADAPT FOR YOUR ACTUAL PROJECT
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

Answered by pallgeuer


This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 4.0.

people found this article helpful. What about you?