27

アプリケーションを起動しようとすると強制的に閉じられ、「setContentView(R.layout.Menu);」という行を指すエラーが表示されます。レイアウトの。XMLファイルでは、レイアウトに「OutOfMemoryError」画像ビューが表示されます。私は本当に混乱しています。更なる移動への導きをお願いします。

編集:

私のアプリケーションはデータベースを使用しており、最初にいくつかの XML データを解析して Sqlite データベースに挿入します。メモリ不足の問題は初回のみ発生します。2回目はうまくいきます。System.gc() を試しました。それについて何か問題はありますか。

これは私のログです:

E/dalvikvm-heap(2712): 105376-byte external allocation too large for this process.
VM won't let us allocate 105376 bytes

    FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Test/com.Test.Menu}: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
    at android.app.Activity.setContentView(Activity.java:1657)
    at com.Test.Menu.onCreate(Menu.java:32)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    ... 11 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
    at android.view.LayoutInflater.createView(LayoutInflater.java:505)
    ... 23 more
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
    at android.content.res.Resources.loadDrawable(Resources.java:1709)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
    at android.widget.ImageView.<init>(ImageView.java:118)
    at android.widget.ImageView.<init>(ImageView.java:108)

これは私のXMLコードです:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
    android:id="@+id/RL_Title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="8"
    android:onClick="onTitleClick" >

    <ImageView
        android:id="@+id/Img_Title_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="center"
        android:src="@drawable/title_bg" />

    <Button
        android:id="@+id/Btn_Title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:background="@drawable/title_al"
        android:drawableRight="@drawable/pro"
        android:gravity="center"
        android:onClick="onTitleClick" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/RL_MainMenu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" android:onClick="onDoNothing">

    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:src="@drawable/main_bg" android:scaleType="centerCrop"/>

    <ImageView
        android:id="@+id/Img_logo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:scaleType="center"
        android:src="@drawable/logo_al" />

    <LinearLayout
        android:id="@+id/LI_Menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/RL_ExtraOption"
        android:layout_alignTop="@+id/Img_logo"
        android:layout_margin="2dp"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/Img_Buyer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:background="@drawable/bt_blink"
            android:onClick="Nextclick"
            android:scaleType="fitCenter"
            android:soundEffectsEnabled="true"
            android:src="@drawable/buyer_icon" />

        <ImageButton
            android:id="@+id/Img_Seller"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:background="@drawable/bt_blink"
            android:onClick="Nextclick"
            android:scaleType="fitCenter"
            android:src="@drawable/seller_icon" />

        <ImageButton
            android:id="@+id/Img_Lender"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:background="@drawable/bt_blink"
            android:onClick="Nextclick"
            android:scaleType="fitCenter"
            android:src="@drawable/lender_icon" />

        <ImageButton
            android:id="@+id/Img_myTitleRep"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:background="@drawable/bt_blink"
            android:onClick="Nextclick"
            android:scaleType="fitCenter"
            android:src="@drawable/my_title_rep_icon_al" />

        <ImageButton
            android:id="@+id/Img_Setup"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:background="@drawable/bt_blink"
            android:onClick="Nextclick"
            android:scaleType="fitCenter"
            android:src="@drawable/setup_icon" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/RL_ExtraOption"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/main_bottom_bg" >

        <TextView
            android:id="@+id/txt_RepName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:textColor="@color/white"
            android:textSize="@dimen/font_size" />

        <TableRow
            android:id="@+id/TR_ContactRep"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:gravity="center" >

            <Button
                android:id="@+id/Btn_ContactRep"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="3dp"
                android:background="@drawable/contact_rep_blink"
                android:onClick="ContactRep_Click" />

            <Button
                android:id="@+id/Btn_MoreOption"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:background="@drawable/main_more_blink"
                android:onClick="onMoreClick" />
        </TableRow>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/ln_Mainmore"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dialog_bg" android:layout_alignParentBottom="true" android:visibility="gone">

        <LinearLayout
            android:id="@+id/LinearLayout02"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:orientation="vertical" >
        </LinearLayout>

        <TableLayout
            android:id="@+id/TableLayout01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" >

            <TableRow
                android:id="@+id/TableRow04"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_marginTop="20dp"
                android:gravity="center" >

                <Button
                    android:id="@+id/Btn_Rate"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/property_blue_blink"
                    android:onClick="onRate"
                    android:singleLine="true"
                    android:text="Rate/Testimonial"
                    android:textColor="@color/white"
                    android:textSize="@dimen/font_size"
                    android:textStyle="bold" />
            </TableRow>

            <TableRow
                android:id="@+id/TableRow01"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:gravity="center" >

                <Button
                    android:id="@+id/btn_SubFeature"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/property_blue_blink"
                    android:onClick="onSubFeature"
                    android:singleLine="true"
                    android:text="Submit A Feature"
                    android:textColor="@color/white"
                    android:textSize="@dimen/font_size"
                    android:textStyle="bold" />
            </TableRow>

            <TableRow
                android:id="@+id/TableRow03"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:gravity="center" >

                <Button
                    android:id="@+id/Btn_ReferFrnd"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/property_blue_blink"
                    android:onClick="onReferAFrnd"
                    android:text="Refer A Friend"
                    android:textColor="@color/white"
                    android:textSize="@dimen/font_size"
                    android:textStyle="bold" />
            </TableRow>

            <TableRow
                android:id="@+id/TableRow02"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:gravity="center" >

                <Button
                    android:id="@+id/Btn_cancel"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/property_cancel_blink"
                    android:onClick="onClose"
                    android:text="Cancel"
                    android:textColor="@color/black"
                    android:textSize="@dimen/font_size"
                    android:textStyle="bold" />
            </TableRow>
        </TableLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

4

8 に答える 8

62

問題はレイアウトにあるとは思いません。問題はコードの別の場所にあります。そして、おそらくどこかでコンテキストを漏らしています

他の考えられる理由は、XML の解析中にかさばる複数のオブジェクトを作成する必要があることです (これは、XML を解析するときに初めて発生すると述べたように)。Javaには自動ガベージコレクションのアプローチがありますが、それでも完全に信頼することはできません。コレクション インスタンスが不要になった場合は、オブジェクトのコンテンツを無効にするか、オブジェクトのコンテンツをクリアすることをお勧めします。

それでも、Android でビットマップを扱う際に覚えておくべき重要なポイントのリストを用意しました。

1)各ビットマップでリサイクルを呼び出し、それらをnullに設定できます。(bitmap.recycle()このビットマップで使用されているすべてのメモリを解放しますが、ビットマップ オブジェクトは無効になりません)。

2)drawablesアクティビティが破棄されたときに、関連するレイアウトをアンバインドすることもできます。以下のコードを試して、このリンクlinkも見てください。

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

// Call this method from onDestroy()

    void onDestroy() {
        super.onDestroy();
        unbindDrawables(findViewById(R.id.RootView));
        System.gc();
    }

3)を に変換hashmapsしてWeakHashmaps、システムのメモリが不足したときにメモリが解放されるようにすることができます。

4)すべてのビットマップをスケーリング/サイズ変更できます。ビットマップをスケーリングするには、次のようなことを試すことができます。

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    Bitmap preview_bitmap=BitmapFactory.decodeStream(is, null, options);

この inSampleSize オプションは、メモリ消費を削減します。

これが完全な方法です。最初に、コンテンツ自体をデコードせずに画像サイズを読み取ります。次に、最適な inSampleSize 値を見つけます。それは 2 のべき乗でなければなりません。そして最後に画像がデコードされます。

// Decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        // The new size we want to scale to
        final int REQUIRED_SIZE=70;

        // Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(o.outWidth/scale/2 >= REQUIRED_SIZE && o.outHeight/scale/2 >= REQUIRED_SIZE)
            scale*=2;

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    }
    catch (FileNotFoundException e) {
    }
    return null;
}

このリンクを見てください。.

5)onLowMemory()システム全体のメモリが不足しているときに呼び出されるアクティビティのメソッドをオーバーライドできます。そこでいくつかのリソースを解放できます。

6)オブジェクトSoftReferenceまたはを作成Weakreferenceして、メモリ不足の状態で解放されるようにすることができます。

私が観察した非常に一般的なメモリ リークは、内部クラスの実装と での実装によるものHandlerですActivityこのリンクでは、同じことについて詳しく説明しています

これがあなたの問題を解決するのに役立つことを願っています.

于 2012-05-02T06:40:16.690 に答える
6

これは、Android のImageView古いデバイスで大きな画像を使用する場合の典型的な問題です (ファイル サイズが数 Kbs であっても)。画像のデコードに使用されるskiaと呼ばれるネイティブ グラフィック ライブラリは、画像のサイズに基づいてネイティブ メモリを割り当てるため、ファイル サイズはこれに直接影響しません。

XML レイアウト定義で -attributeを省略して、メソッドsrcで自分で画像をロードしてもonCreate()、同じエラーが発生します。これを回避する唯一の方法は、デコード中にinSampleSizeinBitmapFactory.decodeStreamを使用してダウンサンプリングを直接実行することです (たとえば、イメージをビットマップ オブジェクトにロードする際の奇妙なメモリ不足の問題を参照してください)。これ自体が、この倍率を決定する方法の問題を提起します。

大きな画像を処理するときに私が見つけた最良の解決策は、 を に置き換えて、ImageView次のWebViewような最小限の HTML をロードすることです。

webView.loadUrl("file:///android_res/raw/background.html");

の内容は次のraw/background.htmlようになります。

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <style type="text/css">
        body {
            margin: 0;
        }

        img {
            width: 100%;
            height: auto;
            display: block;
        }
    </style>
</head>
<body>
    <img src="file:///android_res/drawable/background.png" />
</body>

WebView で使用される WebKit エンジンは、はるかに効率的な方法で画像の読み込みを実行するため、これらのメモリ割り当てエラーはなくなります。

drawable-portと に入れることで、縦向きと横向きに異なる画像を使用することもできますdrawable-landclearCache(false)ただし、メソッドで WebView インスタンスを呼び出す必要があります。onDestroy()そうしないと、最初の読み込み時にキャッシュされた画像が常に使用されるためです (WebView インスタンスでのキャッシュの無効化は機能しません)。

于 2012-05-06T14:15:24.950 に答える
6

この問題に多くの時間を費やした後、私は解決策を見つけました! あなたがする必要があるのは、より多くのメモリを要求することだけです。このコードを Android マニフェスト内の次の場所に配置します。

<application>:

置く:

android:largeHeap="true"

私にとってはうまくいきました!このソリューションは、API 11 以降でのみ機能することに注意してください。

詳細はこちら。

于 2015-11-16T11:00:14.830 に答える
4

人々がここに書いたことに加えて、私はグーグルのこのビデオを見ることをお勧めします。それはいくつかの一般的な記憶の問題とあなたの記憶の問題の原因を見つける方法を指摘しています:

Androidアプリのメモリ管理

于 2012-05-05T18:05:23.710 に答える
4

多くのデバイスでは、各デバイスで使用できるメモリが非常に限られており、制限されています。古いデバイスの多くはアプリケーションに 16MB のメモリを割り当てますが、最新のデバイスでは 24MB または 32MB を割り当てる場合もあります (14MB しかないデバイスを見たことがあります)。

非常に多くの画像をメモリにロードすると、この領域がすぐにいっぱいになり、OutOfMemoryError がスローされます。この問題を克服するには、(以前の回答で述べたように)画像をサムネイルに縮小し、サムネイルのみをデバイスメモリに保持し(メモリフットプリントを削減)、画像をSDCARDに保存する必要があります。

Eclipse の DDMS レイアウトから、プログラムが使用しているメモリの量を確認できます。Application クラスをオーバーライドして、「メモリ不足」警告関数をリッスンすることもできると思います。

BitmapFactory を介してビットマップを読み込むときは、BitmapFactory.Option inScaled パラメータを 2 または 4 (または適切に機能するもの) に設定して、より小さく、メモリ消費の少ないイメージを作成します。

通常、解決策は 1 つだけです。小さい画像を使用することです。コードがなければ、正確に何が間違っているのかを判断するのは困難です。画像サイズを小さくしてみてください。

于 2012-04-30T10:47:32.113 に答える
0

上記のすべての解決策を試したことがあるかもしれませんが、それでも試してみてください。うまくいくまで解決策を見つけるのに迷いました。これはあなたのケースでは夜に働くかもしれません(その場合はすみません)。

myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK); myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

アクティビティを呼び出す前にこれらを配置してください。

于 2013-09-05T00:11:58.057 に答える