在 MainActivity.java 中,我扩展了 TabActivity 以使用 Tabhost。
public class MainActivity extends TabActivity
在每个选项卡中,我使用 ActivityGroup 来管理一些子活动
public class MerchandiserTabGroupActivity extends ActivityGroup
在子活动 A 中,我想显示 DialogFragment。但它不起作用。这是错误:
07-30 11:40:10.430: E/AndroidRuntime(30910): FATAL EXCEPTION: main
07-30 11:40:10.430: E/AndroidRuntime(30910): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@40570598 is not valid; is your activity running?
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.view.ViewRoot.setView(ViewRoot.java:542)
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:203)
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:117)
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.app.Dialog.show(Dialog.java:241)
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.support.v4.app.DialogFragment.onStart(DialogFragment.java:385)
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.support.v4.app.Fragment.performStart(Fragment.java:1336)
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907)
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.os.Handler.handleCallback(Handler.java:587)
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.os.Looper.loop(Looper.java:130)
07-30 11:40:10.430: E/AndroidRuntime(30910): at android.app.ActivityThread.main(ActivityThread.java:3691)
07-30 11:40:10.430: E/AndroidRuntime(30910): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 11:40:10.430: E/AndroidRuntime(30910): at java.lang.reflect.Method.invoke(Method.java:507)
07-30 11:40:10.430: E/AndroidRuntime(30910): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
07-30 11:40:10.430: E/AndroidRuntime(30910): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
07-30 11:40:10.430: E/AndroidRuntime(30910): at dalvik.system.NativeStart.main(Native Method)
我想我必须在它的父活动中显示 DialogFragment。但是很方便。还有其他解决方案吗?
编辑
public void onClick(View view) {
// TODO Auto-generated method stub
switch(view.getId()){
....
case R.id.img_delete_all_photos:
if(mTripSurveryImage.size() > 0){
DialogFragment dialogFragment = new EAlertDialog(
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case Dialog.BUTTON_POSITIVE:
mTripSurveryImage.clear();
imgNoPhoto.setVisibility(View.VISIBLE);
mViewPager.setVisibility(View.GONE);
dialog.dismiss();
break;
case Dialog.BUTTON_NEGATIVE:
dialog.dismiss();
break;
default:
break;
}
}
}, "Warning!!!", mContext.getString(android.R.string.no),
mContext.getString(android.R.string.yes),
"Do you want to delete all the images?");
dialogFragment.show(this.getSupportFragmentManager(), null);
}
break;
}
这是我的 EAlertDialog:
public class EAlertDialog extends DialogFragment implements
android.view.View.OnClickListener {
public static final String TAG = "WaitingDialog";
private String mTitle;
private String mNegative;
private String mPositive;
private String mMessage;
private OnClickListener mOnClickListener;
public EAlertDialog(){
}
public EAlertDialog(OnClickListener listener, String title,
String negative, String positive, String message) {
mTitle = title;
mNegative = negative;
mPositive = positive;
mMessage = message;
mOnClickListener = listener;
setCancelable(false);
setStyle(STYLE_NO_TITLE, 0);
}
// there is only negative button
public EAlertDialog(OnClickListener listener, String title,
String navigative, String message) {
this(listener, title, navigative, null, message);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = null;
if (mPositive == null) {
layout = inflater.inflate(R.layout.alert_dialog_negative, null);
} else {
layout = inflater.inflate(R.layout.alert_dialog, null);
layout.findViewById(R.id.btn_positive).setOnClickListener(this);
((TextView) layout.findViewById(R.id.btn_positive))
.setText(mPositive);
}
layout.findViewById(R.id.btn_negative).setOnClickListener(this);
((TextView) layout.findViewById(R.id.btn_negative)).setText(mNegative);
((TextView) layout.findViewById(R.id.dialog_title)).setText(mTitle);
((TextView) layout.findViewById(R.id.message)).setText(mMessage);
return layout;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return super.onCreateDialog(savedInstanceState);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_negative:
if (mOnClickListener != null) {
mOnClickListener.onClick(getDialog(), Dialog.BUTTON_NEGATIVE);
} else {
dismiss();
}
break;
case R.id.btn_positive:
if (mOnClickListener != null) {
mOnClickListener.onClick(getDialog(), Dialog.BUTTON_POSITIVE);
} else {
dismiss();
}
default:
break;
}
}
}
这样做的目的是重用 EAlertDialog。
提前致谢。