I want to create a normal AlertDialog. I used the example provided by the android dev docs. I just changed the DIALOG_PAUSED_ID to DIALOG_DELETEDB. If I execute my code and press the button which in return should create the dialog, I get the following error log:
04-29 01:01:20.973: WARN/dalvikvm(1168): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-29 01:01:20.973: ERROR/AndroidRuntime(1168): Uncaught handler: thread main exiting due to uncaught exception
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): java.lang.IllegalArgumentException: Activity#onCreateDialog did not create a dialog for id 4
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at android.app.Activity.createDialog(Activity.java:871)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at android.app.Activity.showDialog(Activity.java:2483)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at mjb.project.AVV.Favs.onMenuItemSelected(Favs.java:111)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:525)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at android.view.View.onTouchEvent(View.java:4179)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at android.widget.TextView.onTouchEvent(TextView.java:6540)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at android.view.View.dispatchTouchEvent(View.java:3709)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at android.os.Looper.loop(Looper.java:123)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at java.lang.reflect.Method.invoke(Method.java:521)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-29 01:01:20.993: ERROR/AndroidRuntime(1168): at dalvik.system.NativeStart.main(Native Method)
so here are the "relevant" code parts:
define the ID:
private static final int DELETE_DB_ID = 3;
private Dialog dialog;
static final int DIALOG_DELETEDB = 4;
onCreateDialog(...):
protected Dialog onCreateDialog(int id) {
switch(id) {
case DIALOG_DELETEDB:
// do the work to define the pause Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Favs.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
break;
default:
dialog = null;
}
return dialog;
}
Here I try to "create" the dialog:
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case ADD_ID:
createNote();
return true;
case DELETE_DB_ID:
showDialog(DIALOG_DELETEDB);
return true;
}
return super.onMenuItemSelected(featureId, item);
}
As I already said, I just copied the code and changed the na开发者_C百科me. Unfortunately, I don't understand the error log message.. :/ Somehow I think I don't return the created dialog, but I cannot see "where" my reference is or where/what I have to return...
thanks in advance for help.
- You should not call alert.show() in onCreateDialog.
- Your onCreateDIalog returns dialog variable that is actually null, you don't initialize it.
Please take a look at samples\ApiDemos\src\com\example\android\apis\app\AlertDialogSamples.java, it is done correctly there. You may also read this http://developer.android.com/guide/topics/ui/dialogs.html on correct onCreateDialog usage. Here's the fixed version of your onCreateDialog:
protected Dialog onCreateDialog(int id) {
switch(id) {
case DIALOG_DELETEDB:
// do the work to define the pause Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Favs.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
return alert;
}
return null;
}
Another approach is to display dialog directly:
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case DELETE_DB_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Favs.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
Don't forget to import android.app.AlertDialog first.
AlertDialog alert = new AlertDialog.Builder(this).create();
alert.setTitle("Error");
alert.setMessage("Sorry, your device doesn't support flash light!");
alert.setButton(Dialog.BUTTON_POSITIVE,"OK",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alert.show();
精彩评论