Your original code instantiated a subclass of Activity
(DialogGenerator). Don't do this - Activity (and Service and other
things) are instantiated and managed by Android. If you do, those
objects won't be properly connected to the internals of Android, as you
already discovered.
I don't see anything wrong with a helper method that you switched to -
but if you wanted to make it prettier, you could:
1 - put it into a helper class as a static method:
Dialog dlg = DialogFactory.create(....);
2 - make DialogGenerator take an Activity parameter:
Dialog dlg = new DialogGenerator(this).create(...);
To me it seems just a matter of taste, don't think one is better than
any other :)
-- Kostya
21.05.2011 15:45, luiX_ ?????:
well... just figured it out :P
I've just changed the create dialog method like this (in bold the
parts changing):
public Dialog create(*final Activity activity*, String title,
String message, int icon, final int dialogIdCode) {
AlertDialog.Builder builder = new AlertDialog.Builder(*activity*);
builder.setCancelable(false)
.setTitle(title)
.setIcon(icon)
.setMessage(message)
.setPositiveButton(R.string.accept, new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
*activity*.dismissDialog(dialogIdCode);
}
});
Dialog dialog = builder.create();
return dialog;
}
Anyway, is there any better way on doing this? Thanks!
2011/5/21 luiX_ <lui...@gmail.com <mailto:lui...@gmail.com>>
Hi,
I was trying to have a class for generating a Dialog that is quite
common in my app, the idea is to call this method from different
classes onCreateDialog method, so I wrote this piece of code:
public class DialogGenerator extends Activity {
public static final int DIALOG = 33;
public Dialog create(Context context, String title, String
message, int icon, final int dialogIdCode) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false)
.setTitle(title)
.setIcon(icon)
.setMessage(message)
.setPositiveButton(R.string.accept, new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
*dismissDialog(dialogIdCode); // here is where I'm having trouble*
}
});
Dialog dialog = builder.create();
return dialog;
}
}
then, I wrote this piece for the onCreateDialog method:
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
Dialog dialog = null;
DialogGenerator dg = new DialogGenerator();
Log.e("DEBUG", "Devolvemos dialog...");
dialog = dg.create(this, "titulo", "mensaje", R.drawable.icon,
DialogGenerator.DIALOG);
return dialog;
}
and then call the showDialog like this:
showDialog(DialogGenerator.DIALOG);
The dialog shows up right, but the problem I'm having is that I
get an IllegalArgumentException when dismissing:
05-21 13:33:17.163: ERROR/AndroidRuntime(6114):
java.lang.IllegalArgumentException: no dialog with id 33 was
ever shown via Activity#showDialog
Any ideas on how this can be solved? is there any tutorial on how
to keep all the dialog in one independent class?
Thank you guys!
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Kostya Vasilyev -- http://kmansoft.wordpress.com
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en