我正在尝试使用以下代码设置警告对话框,但应用程序在这一点上不断崩溃。有任何明显的错误吗?
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.
答案 0 :(得分:0)
我认为你的问题可能来自于你是如何做到的。我从android docs
得到这个// 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();
那样创建它时,你并没有真正设置它。你应该做最后创建。然后展示它。
答案 1 :(得分: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();
检查..
答案 2 :(得分: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);
答案 3 :(得分: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