简体   繁体   中英

show force close error in android phone

when i run my app in emulator its work perfectly fine but when i run it in my android phone it show force close error.

Here is my logcat

08-03 02:04:36.602: E/AndroidRuntime(15464): FATAL EXCEPTION: main
08-03 02:04:36.602: E/AndroidRuntime(15464): java.lang.OutOfMemoryError: (Heap Size=47623KB, Allocated=39485KB)
08-03 02:04:36.602: E/AndroidRuntime(15464):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-03 02:04:36.602: E/AndroidRuntime(15464):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:626)
08-03 02:04:36.602: E/AndroidRuntime(15464):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:730)
08-03 02:04:36.602: E/AndroidRuntime(15464):    at com.example.fatwallet.MyAdapter.setDrawable(MyAdapter.java:83)
08-03 02:04:36.602: E/AndroidRuntime(15464):    at com.example.fatwallet.MyAdapter.getView(MyAdapter.java:69)
08-03 02:04:36.602: E/AndroidRuntime(15464):    at android.widget.AbsListView.obtainView(AbsListView.java:2334)
08-03 02:04:36.602: E/AndroidRuntime(15464):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1409)``

line no 83 is...

setDrawable(image, dataObj.getImageId());

and line no 69 is..

private void setDrawable(ImageView image, String drawableName) {
        AssetManager manager = image.getContext().getAssets();
        InputStream open = null;
        try {
            open = manager.open(drawableName+".jpg");
            Bitmap bitmap = BitmapFactory.decodeStream(open);
            // Assign the bitmap to an ImageView in this layout
            image.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (open != null) {
                try {
                    open.close();
                } catch (IOException e) {
                    e.printStackTrace();

Since phones have different memeory sizes my suggestion to you is reduce the image size and try Again. Bitmap createScaleBitmap

You're trying to map a big file into memory. Try to set this in your manifest:

<application 
             android:largeHeap="true">
    . . .
</application>

However it is a discouraged solution. From official documentation:

Most apps should not need this and should instead focus on reducing their overall memory usage for improved performance. Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.

Link here .

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