0

我正在尝试使用下面的代码设置一个警报对话框,但应用程序在这一点上不断崩溃。有什么明显的错误吗?

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.
4

4 回答 4

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();您并没有真正在其中设置任何东西。你应该最后创建。然后展示它。

于 2012-12-21T07:03:56.670 回答
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();

核实..

于 2012-12-21T07:04:36.177 回答
0

感谢@Andro Selva 的回答 内存问题是由图标引起的。如果我注释掉代码的图标行,那么对话框运行良好。

如果我像这样调整图标的大小,那么整个事情运行良好

    //---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);
于 2012-12-21T07:08:19.530 回答
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();

更多细节请参考这个博客

于 2012-12-21T07:08:34.693 回答