Question

I have hit a wall on this one. My DialogFragment works well with all other dialogs I have except for the one that uses a customer Adapter. When changing orientation the second time I get a java.lang.IllegalStateException: Fragment NewAlertDialog{447bc528} not attached to Activity This is using the API 4+ Support package.

It doesn't happen on the first orientation change, it always happens on the second, meaning it happens in this order with the dialog showing:

  • Portrait -> Landscape -> Portrait -> java.lang.IllegalStateException
  • Landscape -> Portrait -> Landscape -> java.lang.IllegalStateException

Here is the dialog:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
        final DialogItem[] items = {
                new DialogItem(getString(R.string.test1), R.drawable.test1),
                new DialogItem(getString(R.string.test2), R.drawable.test2),
            };
        ListAdapter adapter = new ArrayAdapter<DialogItem>(getActivity(),
            android.R.layout.select_dialog_item,
            android.R.id.text1,
            items){
                public View getView(int position, View convertView, ViewGroup parent) {
                    View v = super.getView(position, convertView, parent);
                    TextView tv = (TextView)v.findViewById(android.R.id.text1);
                    tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);
                    int dp10 = (int) (10 * getResources().getDisplayMetrics().density + 0.5f);
                    tv.setCompoundDrawablePadding(dp10);
                    return v;
                }
            };
        return new AlertDialog.Builder(getActivity())
                    .setTitle(R.string.title)
                    .setIcon(R.drawable.icon)
                    .setAdapter(adapter, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                            if (item == 0)
                                doThis();
                            else
                                doThat();
                        }
                    }).create();
}

This is a DialogItem:

class DialogItem {
    public final String text;
    public final int icon;
    public DialogItem(String text, Integer icon) {
        this.text = text;
        this.icon = icon;
    }
    @Override
    public String toString() {
        return text;
    }
}

I know it is a problem with containing an Adapter because if I remove the .setAdapter() call from AlertDialog.Builder then the problem goes away.

Also odd is that there is NO PROBLEM on my ICS device. This only happens on the Gingerbread device I test on. Any help is greatly appreciated!

Thank you!

Matt.

Was it helpful?

Solution

Problem solved. Calling getResources() resources off the Activity instead of the DialogFragment was the necessary change.

Before:

int dp10 = (int) (10 * getResources().getDisplayMetrics().density + 0.5f);

After:

int dp10 = (int) (10 * getActivity().getResources().getDisplayMetrics().density + 0.5f);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top