0

アプリで多くのメモリの問題が発生しています。メモリ例外をキャッチしてから、トーストを使用してメモリ不足というメッセージを表示しようとしています。

テイストコードを使用してアプリを実行すると、クラッシュします。デバッガーで実行すると、クラッシュしませんが、メッセージが表示されません。

ActivtyクラスのonCreateメソッド内のcatchmemory例外内にメッセージを表示しようとしています。

コードブロック:

public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);


            // set size of card image
            ImageView CardImage = ( ImageView)findViewById(R.id.viewcard);
            int ScreenWidth=getWindowManager().getDefaultDisplay().getWidth();
            int ScreenHeight=getWindowManager().getDefaultDisplay().getHeight();

            // fix for tblets
            if (ScreenHeight<ScreenWidth)
               ScreenWidth=ScreenHeight;

            int CardWidth=ScreenWidth/2;
            CardWidth+=CardWidth/10;

            // set bitmap
        //    Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.card2);
            String FileName=new String("card");
            FileName+=Integer.toString( CardId+1);
            FileName+=".jpg";


            // put in text that matches card
            TextView mCardText = (TextView)findViewById(R.id.cardtext);
            mCardText.setText( "Card Meaning \n"+ cCardText.mCardMeaning[ CardId]);

            // put in text that matches card
            TextView mHouseText = (TextView)findViewById(R.id.housetext);
            mHouseText.setText( "House Meaning \n"+ cCardText.mHouseMeaning[ HouseId-1]);

        try {
       AssetManager assetManager= getAssets();
       InputStream inputStream;
       inputStream=assetManager.open(FileName);  
       Bitmap icon=BitmapFactory.decodeStream(inputStream);
       CardImage.setImageBitmap(icon);
        } catch( OutOfMemoryError e)
        {
    /////////////////////////////////////////////////////////////////////
    // crashes heare
            Context context = getApplicationContext();
            CharSequence text = "Out of Memory  ";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();  

            gi++;
              return;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return;
        } 


        //scale it
        // note: only code here that pertains to card image, becouse if card image cannot be loaded
        // the above exceptions will go off and we will never get here
        CardImage.getLayoutParams().width=CardWidth;
        CardImage.getLayoutParams().height = (int)( (float) CardWidth *1.7);


    }
4

1 に答える 1

1

OutOfMemoryの場合、JVMは実際には新しいメモリを割り当てることができないため、すべての賭けはほとんど無効になります。そのため、クラッシュする可能性があります。

メッセージを表示する前にいくつかのポインタをクリアしてみることをお勧めします...しかし、実際にOutOfMemoryエラーをキャッチしようとする人を見たことがありません。

この投稿の3番目の答えJavaでメモリ不足の例外をキャッチすることは可能ですか?メモリ不足の例外をキャッチしようとするのが本当に悪い考えである理由を少し説明します。

于 2012-09-24T22:03:57.063 に答える