[英]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.
有关避免资源泄漏的信息,请参见java.io.Closeable。
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 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. Closable
具有close
方法,当您不再需要该方法时,必须调用该方法以释放与该组件关联的资源。
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). 要查找泄漏,您可以尝试
MAT
,我经常用它来查找内存泄漏(持有对Activity的引用的静态数据等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.