Java 扩展AlertDialog
我需要在AlertDialog中存储一个附加值(标记),所以我扩展了这个类。当使用这个新类时,我确实得到了一个ClassCastException。我的代码怎么了 这是我的扩展警报对话框:Java 扩展AlertDialog,java,android,android-alertdialog,Java,Android,Android Alertdialog,我需要在AlertDialog中存储一个附加值(标记),所以我扩展了这个类。当使用这个新类时,我确实得到了一个ClassCastException。我的代码怎么了 这是我的扩展警报对话框: public class MyAlertDialog extends AlertDialog { private long tag; public MyAlertDialog(final Context context) { super(context); }
public class MyAlertDialog extends AlertDialog {
private long tag;
public MyAlertDialog(final Context context) {
super(context);
}
public MyAlertDialog(final Context context, final int theme) {
super(context, theme);
}
public MyAlertDialog(final Context context, final boolean cancelable, final OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
public long getTag() {
return tag;
}
public void setTag(final long tag) {
this.tag = tag;
}
}
这是使用新类的ListActivity的一部分:
public class MyListActivity extends ListActivity {
private MyAlertDialog alertDialog;
private long id;
@Override
public boolean onContextItemSelected(final MenuItem menuItem) {
// ..
processAlertDialog(id);
// ..
return super.onContextItemSelected(menuItem);
}
private void processAlertDialog(final long id) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setMessage(R.string.txt_reallydelete);
alertDialogBuilder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialogInterface, final int which) {
dialogInterface.dismiss();
}
} );
alertDialogBuilder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialogInterface, final int which) {
dialogInterface.dismiss();
// do Something
}
} );
// ClassCastException
alertDialog = (MyAlertDialog) alertDialogBuilder.create();
alertDialog.setTag(id);
alertDialog.show();
}
}
非常感谢
编辑:
Uncaught handler: thread main exiting due to uncaught exception
java.lang.ClassCastException: android.app.AlertDialog
at com.test.app.MyListActivity.processAlertDialog(MyListActivity.java:234)
at com.test.app.MyListActivity.onContextItemSelected(MyListActivity.java:99)
at android.app.Activity.onMenuItemSelected(Activity.java:2174)
at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2731)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:129)
at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:884)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3285)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
因为在创建“MyAlertDialog”时,并没有创建实际的“MyAlertDialog”对象。 您创建了“AlertDialog.Builder”对象并将其强制转换为“MyAlertDialog” 你为什么这样做 我想你可以像这样创建和显示你自己的对话框
private void processAlertDialog(final long id) {
MyAlertDialog alertdialog = new MyAlertDialog(this);
alertdialog.setCancelable(false);
alertdialog.setMessage(R.string.txt_reallydelete);
// your button's call back
alertdialog.setTag(id);
alertdialog.show();
}
为了使事情变得简单,您应该创建一个扩展
AlertDialog.Builder
的类
public class MyAlertDialog extends AlertDialog.Builder {
//add constructor stuff
private long tag;
public long getTag() {
return tag;
}
public void setTag(final long tag) {
this.tag = tag;
}
}
并通过调用show()
方法在活动中使用它
yourBuilder.show();
您不需要在生成器上调用create(),因为您直接显示它。第234行标记在显示的源中。谢谢。alertDialogBuilder.create()确实返回AlertDialog对象。我确实将其转换为MyAlertDialog。这一行给出了ClassCastException。这行有什么问题?因为您试图将父对象强制转换为子对象。如果您将“MyAlertDialog”强制转换为“AlertDialog”对象,就可以了。因为“MyAlertDialog”扩展了“AlertDialog”。然而,反过来说,JAVA是不允许的。您可以在任何JAVA books.Arg中找到此文件。在所有这些应用程序之后,我仍然不懂Java。我一直在苦苦思索:为什么我可以把更大的(孩子)给更小的(父母)呢。我总是期望相反的结果。然而,我对你的评论投了反对票。谢谢。@HaraldWilhelm这就像“所有的芒果都是水果,但所有的水果都不是芒果”。谢谢。起初这看起来很有希望,但还有一个问题。如果我的标记变量在MyAlertDialogBuilder中,并且我发出show()-而没有存储将返回的AlertDialog-我以后如何关闭该对话框?无论我做什么,我最终都会有一个我需要但不可能的演员阵容。让我解释一下。我想让这些AlertDialogs经受住方向的变化。因此,我将id存储(例如在ListActivity上)在AlertDialog中,并在OnRetainOnConfigurationInstance()期间将此id放在保留对象中并关闭该对话框。不,使用此模型时不需要任何强制转换。您有不同的纵向和横向布局吗?因为如果你不这样做,那么你的对话甚至可以在方向改变的情况下生存下来,而不需要任何进一步的技巧:)再次感谢。我就是这样做的(调用alertDialogBuilder.show()-不创建AlertDialog)。没有自定义布局-只有确定/取消按钮。旋转屏幕后,对话框消失,我得到一个窗口泄漏错误。我怀疑在没有我的交互的情况下,方向改变后是否会再次出现AlertDialog。这是我过去几年的经验。您是否有可能使用清单设置android:configChanges?我不使用此设置,我必须自己重新创建所有对话框。是的,您确实需要将
android:configChanges=“orientation”
设置到您的活动中,以手动处理方向更改(如果需要)。这样做不会提示任何窗口泄漏警告。我不明白。我从不使用安卓:configChanges=“orientation”。方向自动更改后,我的AlertDialogs不会自动重新绘制。你的魔术是什么?