我正在尝试设置一个警告对话框,使用下面的代码,但应用程序在这一点上总是崩溃。有什么明显的错误吗?
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提到了内存,尽管我不明白为什么a对话框会导致内存错误(如果a对话框被注释掉,应用程序运行正常)
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.发布于 2012-12-21 15:08:20
感谢@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-21 15:03:57
我认为你的问题可能来自于你是如何做的。这是我从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-21 15:04:36
U在将视图分配给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();检查一下..
https://stackoverflow.com/questions/13985622
复制相似问题