简体   繁体   中英

Getting Java.io.closeable Error in Android

I am getting the following error. While running my program. I am unable to understand the problem. A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.

Code

ArrayList<String> selection = new ArrayList<>();
TextView finaltext = null;
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
        finaltext = (TextView)findViewById(R.id.final_result);
        finaltext.setEnabled(false);
}
public void selectItem(View view)
{
    boolean checked = ((CheckBox) view).isChecked();
    switch (view.getId())
    {
        case R.id.fruit_apple:if (checked){selection.add("Apple");}else {selection.remove("Apple");}
        break;
        case R.id.fruit_orange:if (checked){selection.add("Orange");}else {selection.remove("Orange");}
        break;
        case R.id.fruit_grape:if (checked){selection.add("Grapes");}else {selection.remove("Grapes");}
        break;
    }
}

XML

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/title_message"
    android:textAppearance="?android:textAppearanceLarge"
    android:layout_gravity="center_horizontal"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fruit_apple"
android:text="@string/apple"
android:onClick="selectItem"/>
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/fruit_orange"
    android:text="@string/orange"
    android:onClick="selectItem"
    />
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/fruit_grape"
    android:text="@string/grapes"
    android:onClick="selectItem"/>
<Button
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:text="done"
    android:layout_gravity="center_horizontal"
    android:onClick="finalSelection"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/final_result"
    android:text="Hello World"/>
</RelativeLayout>

That means you have opened something but never close them. Closable have a method close which you must call to release the resources associated with the component when you no longer need it.

To look for the leak, you can try MAT , I often use it to find memory leaks(static data holding a reference to Activity, etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM