2012-12-21 96 views
0

我試圖建立一個警告對話框,使用下面的代碼,但應用程序崩潰保持在這一點上。任何明顯的錯誤?下面alertdialog導致內存錯誤

AlertDialog alertDialog = new AlertDialog.Builder( 
    Crossword1.this).create(); 
    alertDialog.setTitle("Well done, the crossword is complete!"); 
    alertDialog.setMessage("well done cuz"); 
    alertDialog.setIcon(R.drawable.tick); 
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 

     } 
    }); 
alertDialog.show(); 

logcat中,提到內存雖然我不明白爲什麼alertdialog箱應引起內存錯誤(如果alertdialog被註釋掉的應用程序運行正常)

12-21 17:57:33.180: D/BounceScrollRunnableDefault(24966): start(1554.2593), mBounceExtent:0.0 
    12-21 17:57:33.180: D/BounceScrollRunnableDefault(24966): mBounceExtentCoef = -205.16223 
    12-21 17:57:34.335: D/BounceScrollRunnableDefault(24966): start(0.0), mBounceExtent:-11.0 
    12-21 17:57:34.335: D/BounceScrollRunnableDefault(24966): mBounceExtentCoef = -11.0 
    12-21 17:57:34.555: D/BounceScrollRunnableDefault(24966): start(0.0), mBounceExtent:-19.245642 
    12-21 17:57:34.555: D/BounceScrollRunnableDefault(24966): mBounceExtentCoef = -19.245642 
    12-21 17:57:35.505: I/dalvikvm-heap(24966): Clamp target GC heap from 65.159MB to 64.000MB 
    12-21 17:57:35.505: D/dalvikvm(24966): GC_FOR_ALLOC freed 580K, 2% free 64467K/65479K, paused 20ms 
    12-21 17:57:35.505: I/dalvikvm-heap(24966): Forcing collection of SoftReferences for 2903056-byte allocation 
    12-21 17:57:35.545: I/dalvikvm-heap(24966): Clamp target GC heap from 65.151MB to 64.000MB 
    12-21 17:57:35.545: D/dalvikvm(24966): GC_BEFORE_OOM freed 9K, 2% free 64458K/65479K, paused 27ms 
    12-21 17:57:35.545: E/dalvikvm-heap(24966): Out of memory on a 2903056-byte allocation. 
+2

請發表您的logcat的錯誤! –

+0

道歉,增加上面 – Kurt

+2

你正在超出內存異常(最後一行日誌)。除非您使用的圖標是一個巨大的文件,否則您的警報對話框與它無關。 –

回答

0

感謝@Andro塞爾瓦的答案 內存問題正在由圖標引起。如果我註釋掉圖標代碼行,那麼對話框運行良好。

如果我調整,像這樣的圖標,那麼整個事情運行正常

//---scales the alertdialog items to the right size--- 
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.tick); 
    int width = bitmapOrg.getWidth(); 
    int height = bitmapOrg.getHeight(); 
    int newWidth = 200; 
    int newHeight = 200; 

    // calculate the scale - in this case = 0.4f 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 

    // create matrix for the manipulation 
    Matrix matrix = new Matrix(); 

    // resize the bit map 
    matrix.postScale(scaleWidth, scaleHeight); 

    // rotate the Bitmap 
    matrix.postRotate(0); 

    // recreate the new Bitmap 
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true); 

    // make a Drawable from Bitmap to allow to set the BitMap // to the ImageView, ImageButton or what ever 
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); 
0

我想你問題可能來自於你如何做。我得到這個從Android文檔

// 1. Instantiate an AlertDialog.Builder with its constructor 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

// 2. Chain together various setter methods to set the dialog characteristics 
    builder.setMessage(R.string.dialog_message) 
     .setTitle(R.string.dialog_title); 

// 3. Get the AlertDialog from create() 
    AlertDialog dialog = builder.create(); 

當你像這樣AlertDialog alertDialog = new AlertDialog.Builder( Crossword1.this).create();創造它,你是不是真的進行任何設置在裏面。你最後應該創造。然後顯示它。

0

ü分配的意見,UI之前創建的對話框。該錯誤可能是在這裏::

AlertDialog alertDialog = new AlertDialog.Builder( 
    Crossword1.this).create(); 

是這樣做的..

AlertDialog.Builder builder= new AlertDialog.Builder(
        Crossword1.this) 
             .setTitle("Well done, the crossword is complete!"); 
             .setMessage("....") 
        .setCancelable(false) 
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int whichButton) { 
          //ur code 
         } 
        }); 
      return builder.create(); 

檢查..

0

試試這個代碼..

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
       Crossword1.this); 
       // set title 
       alertDialogBuilder.setTitle("Well done, the crossword is complete!"); 
       // set dialog message 
       alertDialogBuilder 
       .setMessage("well done cuz") 
       .setCancelable(false) 
       .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        // if this button is clicked, 
        // DO YOUR CODE HERE 

        //Here I start new activity 
        Intent i = new Intent(MainActivity.this, abcd.class); 
        startActivity(i); 
       } 
        }) 
       .setNegativeButton("No",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        // if this button is clicked, just close 
        // the dialog box and do nothing 
        dialog.cancel(); 
       } 
       }); 
       // create alert dialog 
       AlertDialog alertDialog = alertDialogBuilder.create(); 
       // show it 
       alertDialog.show(); 

更多細節Refer this blog