0

当我试图在单击按钮时第二次打开它时,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)
4

2 回答 2

1

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

r = 0;
for (Account account : accounts) {
    accountsList = account.name.toString();
    emailAddress[r] = accountsList;
    r += 1;
}
于 2014-02-19T11:46:43.720 回答
0

I believe unable to add window token null is the error you are receiving. And as @Gopal said probable reason is that you are calling,

super.onCreate(savedInstanceState);

or there are chances that you are mistakenly leaking out the reference to the activity somewhere.

Please see the below link for more details on this. Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context

Pardon me if this is not the case.

于 2014-02-19T11:23:06.120 回答