应用FC上重用绘制资源 [英] App FC on reusing drawable resource
问题描述
if (someId.matches("A") || someId.matches("a")) {
addLetters();
addIcon(R.drawable.apple);
addSentence("A is for APPLE");
btnPlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
playTheSound(R.raw.a);
}
});
}
if (someId.matches("B") || someId.matches("b")) {
addLetters();
addIcon(R.drawable.ball);
addSentence("B is for BALL");
btnPlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
playTheSound(R.raw.a);
}
});
}
public void addIcon(int iconResource) {
ivLetterIcon.setImageResource(iconResource);
}
第一次与code加载活动,一切工作正常。我可以选择信 A
并返回到previous活动,回来的信 A
,继续做它和我的应用程序不FC,但是当我选择信 B
或字母旁边任何其他字母 A
我的应用程序FC。
The first time the activity with the code loads, everything works fine. I can select the letter A
and go back to previous activity and come back to letter A
and keep doing it and my app doesn't FC but when I choose the letter B
or any other letter beside letter A
my app FC.
我的LogCat中显示以下内容:
My LogCat shows the following:
08-27 14:58:52.691: E/AndroidRuntime(716): FATAL EXCEPTION: main
08-27 14:58:52.691: E/AndroidRuntime(716): java.lang.OutOfMemoryError
08-27 14:58:52.691: E/AndroidRuntime(716): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.content.res.Resources.loadDrawable(Resources.java:1930)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.content.res.Resources.getDrawable(Resources.java:659)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.widget.ImageView.resolveUri(ImageView.java:611)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.widget.ImageView.setImageResource(ImageView.java:354)
08-27 14:58:52.691: E/AndroidRuntime(716): at com.test.testing.AlpDisplay.addIcon(AlpDisplay.java:548)
08-27 14:58:52.691: E/AndroidRuntime(716): at com.test.testing.AlpDisplay.onCreate(AlpDisplay.java:204)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.app.Activity.performCreate(Activity.java:5008)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.os.Looper.loop(Looper.java:137)
08-27 14:58:52.691: E/AndroidRuntime(716): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-27 14:58:52.691: E/AndroidRuntime(716): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 14:58:52.691: E/AndroidRuntime(716): at java.lang.reflect.Method.invoke(Method.java:511)
08-27 14:58:52.691: E/AndroidRuntime(716): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-27 14:58:52.691: E/AndroidRuntime(716): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-27 14:58:52.691: E/AndroidRuntime(716): at dalvik.system.NativeStart.main(Native Method)
正在这里引发的错误:
The error is being thrown here:
addIcon(R.drawable.ball);
和
public void addIcon(int iconResource) {
ivLetterIcon.setImageResource(iconResource);
}
错误行:
08-27 15:00:20.341: E/AndroidRuntime(760): at com.test.testing.AlpDisplay.addIcon(AlpDisplay.java:548)
08-27 15:00:20.341: E/AndroidRuntime(760): at com.test.testing.AlpDisplay.onCreate(AlpDisplay.java:204)
由于我使用的是仿真器呢?
Is it because I am using an Emulator?
我试图用一个函数每一个字母,以减少程序行。
I am trying to use one function for every letter to cut down on the program lines.
推荐答案
一,我发现开发Android应用程序中最常见的错误是
One of the most common errors that I found developing Android Apps is the
java.lang.OutOfMemoryError
java.lang.OutOfMemoryError
首先,设置你的XML布局的父视图中的id属性:
First, set the "id" attribute on the parent view of your XML layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/RootView">
...
然后,在你活动的的onDestroy()方法调用unbindDrawables()方法传递引用父视图,然后做一个System.gc()的。
Then, on the onDestroy() method of your Activity, call the unbindDrawables() method passing a reference to the parent View and then do a System.gc().
@Override
protected void onDestroy() {
super.onDestroy();
unbindDrawables(findViewById(R.id.RootView));
System.gc();
}
private void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
这为我工作。我希望这有助于:)
This worked for me. I hope this helps :)
这篇关于应用FC上重用绘制资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!