显示带适配器的 AlertDialog 的 DialogFragment 无法在方向更改后幸存
我在这件事上碰壁了。我的 DialogFragment 与我拥有的所有其他对话框配合良好,除了使用客户适配器的对话框之外。第二次更改方向时,我收到 java.lang.IllegalStateException: Fragment NewAlertDialog{447bc528} not Attach to Activity
这是使用 API 4+ 支持包。
它不会在第一次方向更改时发生,它总是在第二次方向更改时发生,这意味着它按此顺序发生,对话框显示:
- 纵向 -> 纵向风景->肖像 -> 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;
}
}
我知道这是包含适配器的问题,因为如果我从 AlertDialog.Builder< 中删除
.setAdapter()
调用/code> 那么问题就消失了。
同样奇怪的是,我的 ICS 设备上没有任何问题。这种情况只发生在我测试的 Gingerbread 设备上。非常感谢任何帮助!
谢谢你!
马特。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题解决了。从 Activity 而不是 DialogFragment 调用 getResources() 资源是必要的更改。
之前:
之后:
Problem solved. Calling getResources() resources off the Activity instead of the DialogFragment was the necessary change.
Before:
After: