扩展AlertDialog

时间:2012-03-31 08:29:58

标签: java android alertdialog

我需要在AlertDialog中存储一个额外的值(标记),所以我确实扩展了这个类。当使用这个新类时,我得到一个ClassCastException。我的代码出了什么问题?

这是我的扩展AlertDialog:

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)

2 个答案:

答案 0 :(得分:1)

因为在创建“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();
}

答案 1 :(得分:1)

为简单起见,您应该创建一个扩展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()方法在您的Activity中使用它。

yourBuilder.show();

您不需要在构建器上调用 create(),因为您直接显示它