当我尝试在Button Click上第二次打开它时,Dialogfragment(Alert-Dialog)崩溃了

时间:2014-02-19 11:14:18

标签: android android-alertdialog android-dialogfragment dialogfragment

当我尝试在按钮点击时第二次打开它时,Dialogfragment崩溃。此Dialogfragment在单个选择单选按钮中显示现有邮件帐户。这是我的代码:

public String[] allemails() {
    _accountMgr = AccountManager.get(getActivity());
    Account[] accounts = _accountMgr.getAccounts();
    numberOfEmail = accounts.length;
    String[] emailAddress = new String[numberOfEmail];

    for (Account account : accounts) {
        accountsList = account.name.toString();
        emailAddress[r] = accountsList;
        r += 1;
    }
    MyAlertDialog f = new MyAlertDialog();
    Bundle args = new Bundle();
    args.putStringArray("Title", emailAddress);
    f.setArguments(args);

    return emailAddress;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    super.onCreate(savedInstanceState);
    setRetainInstance(true);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(Html.fromHtml("<font color='#FFFF'>"
            + "Choose Account" + "</font>"));

    builder.setIcon(R.drawable.ic_udinic);
    builder.setCancelable(false).setSingleChoiceItems(allemails(), -1,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    AlertDialog alertDialog = (AlertDialog) dialog;
                    alertDialog.getButton(-1).setEnabled(true);

                }
            });
    builder.setCancelable(false).setPositiveButton("Set",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int id) {

                    ((AlertDialog) dialog).getButton(id).setVisibility(
                            View.INVISIBLE);

                    ListView lw = ((AlertDialog) dialog).getListView();
                    CharSequence checkedItem = (CharSequence) lw
                            .getAdapter().getItem(
                                    lw.getCheckedItemPosition());

                    // Toast.makeText(getActivity(), checkedItem,
                    // Toast.LENGTH_LONG).show();

                    ((Main) getActivity()).doPositiveClick(checkedItem);
                    // dialog.dismiss();
                    // getDialog().dismiss();

                }
            });

    builder.setCancelable(false).setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog

                    // Toast.makeText(getActivity(),
                    // "You Press Cancel Button", Toast.LENGTH_LONG).show();

                    // dialog.dismiss();

                }

            });

    dialog = builder.create();
    return dialog;
}

@Override
public void onStart() {
    super.onStart();
    dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
}

@Override
public void onDestroyView() {
    // Removes the dismiss message to avoid shown dialogs being dismissed.
    if (getDialog() != null && getRetainInstance())
        getDialog().setDismissMessage(null);

    super.onDestroyView();
}

这是我的Logcat:

    02-19 05:34:06.657: E/AndroidRuntime(1808): FATAL EXCEPTION: main
    02-19 05:34:06.657: E/AndroidRuntime(1808): Process: com.udinic.accounts_authenticator_example, PID: 1808
    02-19 05:34:06.657: E/AndroidRuntime(1808): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at com.mycuratio.app.MyAlertDialog.allemails(MyAlertDialog.java:46)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at com.mycuratio.app.MyAlertDialog.onCreateDialog(MyAlertDialog.java:70)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at android.app.DialogFragment.getLayoutInflater(DialogFragment.java:398)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at android.app.BackStackRecord.run(BackStackRecord.java:684)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at android.os.Handler.handleCallback(Handler.java:733)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at android.os.Handler.dispatchMessage(Handler.java:95)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at android.os.Looper.loop(Looper.java:136)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at android.app.ActivityThread.main(ActivityThread.java:5017)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at java.lang.reflect.Method.invokeNative(Native Method)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at java.lang.reflect.Method.invoke(Method.java:515)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    02-19 05:34:06.657: E/AndroidRuntime(1808):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

for循环之前,将r的值重置为零。

r = 0;
for (Account account : accounts) {
    accountsList = account.name.toString();
    emailAddress[r] = accountsList;
    r += 1;
}

答案 1 :(得分:0)

我相信无法添加窗口令牌null 是您收到的错误。正如@Gopal所说,可能的原因是你在呼唤,

super.onCreate(savedInstanceState);

或者你有可能错误地泄露了某个地方对活动的引用。

有关详细信息,请参阅以下链接。 Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context

如果不是这样,请原谅我。