gpt4 book ai didi

java - App FC 关于重用可绘制资源

转载 作者:行者123 更新时间:2023-12-01 14:11:50 27 4
gpt4 key购买 nike

    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);
}

第一次加载带有代码的 Activity 时,一切正常。我可以选择字母 A并返回上一个 Activity 并返回信件 A并继续这样做,我的应用程序没有 FC,但是当我选择字母 B 时或字母 A 旁边的任何其他字母我的应用程序 FC。

我的 LogCat 显示以下内容:
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)

这里抛出错误:
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)

是因为我使用的是模拟器吗?

我试图为每个字母使用一个函数来减少程序行。

最佳答案

我发现开发 Android 应用程序的最常见错误之一是

java.lang.OutOfMemoryError

首先,在 XML 布局的父 View 上设置“id”属性:

<?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">
...

然后,在 Activity 的 onDestroy() 方法上,调用 unbindDrawables() 方法传递对父 View 的引用,然后执行 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();
}
}

这对我有用。我希望这有帮助 :)

关于java - App FC 关于重用可绘制资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18473527/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com