これで壁にぶち当たりました。私の DialogFragment は、顧客アダプターを使用するものを除いて、私が持っている他のすべてのダイアログでうまく機能します。2 回目に方向を変更すると、java.lang.IllegalStateException: Fragment NewAlertDialog{447bc528} not attached to Activity
This is using the API 4+ Support package が表示されます。
最初の向きの変更では発生しません。2 番目の変更では常に発生します。つまり、次の順序でダイアログが表示されます。
- 縦 -> 横 -> 縦 -> java.lang.IllegalStateException
- 横 -> 縦 -> 横 -> java.lang.IllegalStateException
ダイアログは次のとおりです。
@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();
}
これは 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;
}
}
.setAdapter()
呼び出しを削除すると問題が解消されるため、アダプターを含めることに問題があることはわかっていAlertDialog.Builder
ます。
また、奇妙なことに、私の ICS デバイスには問題がありません。これは、私がテストした Gingerbread デバイスでのみ発生します。どんな助けでも大歓迎です!
ありがとうございました!
マット。