使用ImageView重复打开Activity会导致内存不足错误

时间:2015-01-13 22:43:10

标签: android android-intent imageview repeat

我有一个基本的应用程序,它有一个按钮,通过意图打开一个活动,并传递1到9之间的随机数,用于显示图像。

图像是每个大小约280KB的png文件。

应用程序的一般用途显示结果活动,然后用户点击后退按钮并再次单击该按钮以启动活动以显示另一个随机图像。 这很好,但是,在大约6或7之后,我得到一个与ResultActivity中相应的setBackgroundResource行相关的内存不足错误:

result_image.setBackgroundResource(R.drawable.result2);

我尝试过很多“unbindDrawables”强制垃圾收集的方法,将imageView设置为null等,但似乎没什么用。

当然有了这样一个基本的应用程序,我在这里错过了一些简单的东西?

通过按下按钮调用意图,如下所示:

/** Called when the user clicks the Send button */
    public void displayResult(View view) {
        int randomNum = minimum + (int) (Math.random() * maximum);
        Log.d(TAG, "result" + randomNum);

        Intent intent = new Intent(this, ResultActivity.class);
        intent.putExtra(EXTRA_MESSAGE, randomNum);
        startActivity(intent);
    }

显示图像的活动如下所示

public class ResultActivity extends Activity {
    private ImageView result_image;
    protected static final String TAG = "ResultActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        // Get the message from the intent
        Intent intent = getIntent();
        int message = intent.getIntExtra(ThumbMain.EXTRA_MESSAGE, 1);
        result_image = null;

        result_image = (ImageView) findViewById(R.id.result_image);

        TextView result_title = (TextView) findViewById(R.id.result_title);
        TextView result_description = (TextView) findViewById(R.id.result_description);

        Log.d(TAG, "message=" + message);
        switch (message) {
        case 1:
            result_image.setBackgroundResource(R.drawable.result1);
            result_title.setText(R.string.result1_title);
            result_description.setText(R.string.result1_description);
            break;
        case 2:
            result_image.setBackgroundResource(R.drawable.result2);
            result_title.setText(R.string.result2_title);
            result_description.setText(R.string.result2_description);
            break;
        case 3:
            result_image.setBackgroundResource(R.drawable.result3);
            result_title.setText(R.string.result3_title);
            result_description.setText(R.string.result3_description);
            break;
        case 4:
            result_image.setBackgroundResource(R.drawable.result4);
            result_title.setText(R.string.result4_title);
            result_description.setText(R.string.result4_description);
            break;
        case 5:
            result_image.setBackgroundResource(R.drawable.result5);
            result_title.setText(R.string.result5_title);
            result_description.setText(R.string.result5_description);
            break;
        case 6:
            result_image.setBackgroundResource(R.drawable.result6);
            result_title.setText(R.string.result6_title);
            result_description.setText(R.string.result6_description);
            break;
        case 7:
            result_image.setBackgroundResource(R.drawable.result7);
            result_title.setText(R.string.result7_title);
            result_description.setText(R.string.result7_description);
            break;
        case 8:
            result_image.setBackgroundResource(R.drawable.result8);
            result_title.setText(R.string.result8_title);
            result_description.setText(R.string.result8_description);
            break;
        case 9:
            result_image.setBackgroundResource(R.drawable.result9);
            result_title.setText(R.string.result9_title);
            result_description.setText(R.string.result9_description);
            break;
        default:
            result_image.setBackgroundResource(R.drawable.result1);
            break;
        }

    }
}

活动的xml在这里:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="org.example.android.ResultActivity"
    android:id="@+id/top_layout" >

    <ImageView
        android:id="@+id/result_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/result_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/result_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

堆栈追踪:

01-13 22:44:20.545: E/AndroidRuntime(29561): FATAL EXCEPTION: main
01-13 22:44:20.545: E/AndroidRuntime(29561): Process: org.example.android.exampletest, PID: 29561
01-13 22:44:20.545: E/AndroidRuntime(29561): java.lang.OutOfMemoryError
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.content.res.Resources.loadDrawable(Resources.java:3426)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.content.res.Resources.getDrawable(Resources.java:1898)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.view.View.setBackgroundResource(View.java:16235)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at org.example.android.ResultActivity.onCreate(ResultActivity.java:50)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.app.Activity.performCreate(Activity.java:5451)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.app.ActivityThread.access$900(ActivityThread.java:172)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.os.Looper.loop(Looper.java:136)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at android.app.ActivityThread.main(ActivityThread.java:5586)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at java.lang.reflect.Method.invokeNative(Native Method)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at java.lang.reflect.Method.invoke(Method.java:515)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
01-13 22:44:20.545: E/AndroidRuntime(29561):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

来自samgak的评论 - &#34;尝试将您的图片放入drawable-nodpi文件夹而不是drawable&#34;解决了这个问题。

非常感谢。