2

我正在使用 Swipey-ViewPagerIndicator。我有三个片段。这些片段的布局包含 ImageView。这是fragment_swipeytab.xml:

<?xml version="1.0" encoding="utf-8"?>
<!--
    Copyright 2011 Peter Kuterna

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff" 
    android:id="@+id/RootView">

   <ImageView
       android:id="@+id/imageView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
        />

</FrameLayout>

以编程方式为三个片段设置图像。这是 SwipeyTabFragment.java:

 /*
     * Copyright 2011 Peter Kuterna
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */

    package com.gmail.suankap.firstapp;

    import java.io.IOException;

    import net.peterkuterna.android.apps.swipeytabs.R;
    import android.content.Intent;
    import android.content.res.AssetFileDescriptor;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnPreparedListener;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.v4.app.Fragment;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.MediaController;
    import android.widget.Toast;
    import android.widget.MediaController.MediaPlayerControl;
    import android.widget.TextView;

    public class SwipeyTabFragment extends Fragment {

        public static Fragment newInstance(String title) {
            SwipeyTabFragment f = new SwipeyTabFragment();
            Bundle args = new Bundle();
            args.putString("title", title);
            f.setArguments(args);
            return f;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            ViewGroup root = (ViewGroup) inflater.inflate(
                    R.layout.fragment_swipeytab, null);
            final String title = getArguments().getString("title");
            ((ImageView) root.findViewById(R.id.imageView1))
                    .setImageResource(R.drawable.both);
            Log.v("TAGS", title);
            return root;
        }

    }

这是Logcat:

 09-28 08:41:25.546: E/AndroidRuntime(376): FATAL EXCEPTION: main
    09-28 08:41:25.546: E/AndroidRuntime(376): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.content.res.Resources.loadDrawable(Resources.java:1709)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.content.res.Resources.getDrawable(Resources.java:581)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.widget.ImageView.resolveUri(ImageView.java:501)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.widget.ImageView.setImageResource(ImageView.java:280)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at com.gmail.suankap.firstapp.SwipeyTabFragment.onCreateView(SwipeyTabFragment.java:59)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:837)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1041)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:616)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1359)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:422)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:86)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.view.ViewPager.populate(ViewPager.java:453)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:696)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:668)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewGroup.drawChild(ViewGroup.java:1562)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.View.draw(View.java:6883)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.View.draw(View.java:6883)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewRoot.draw(ViewRoot.java:1522)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.os.Handler.dispatchMessage(Handler.java:99)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.os.Looper.loop(Looper.java:123)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at java.lang.reflect.Method.invokeNative(Native Method)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at java.lang.reflect.Method.invoke(Method.java:507)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    09-28 08:41:25.546: E/AndroidRuntime(376):  at dalvik.system.NativeStart.main(Native Method)
    09-28 08:41:28.314: I/Process(376): Sending signal. PID: 376 SIG: 9

问题是 ANR - 当我滑动片段时出现 OutOfMemory 错误。当只有三个片段中只有一个图像(也是相同的资源 R.drawable.both )时,为什么会发生 OutOfMemory ?如何解决?

4

3 回答 3

1

您的一张图片占用 1944x1944*4 字节 == 14.5 MB。现在,您的三张图片最多只占用 43.5 MB。根据设备,应用程序的分配空间从 32 MB 到 64 MB。如果是同一张图片,您也可以将其设为 Activity 的背景,而不是片段。

于 2013-09-28T04:27:12.490 回答
0

你的图像 R.drawable.both 的大小是多少?

于 2013-09-28T02:47:40.990 回答
0

我在显示图像时遇到了同样的问题。尝试使用一个非常小的图像,看看它是否有效。我最终不得不将我的图像大小调整为大约 800w x 1000h,以使应用程序不会崩溃。这个线程帮助了我:Android:Gallery 中的内存不足异常

于 2013-09-28T04:13:45.440 回答