在MainActivity.java中,我擴展TabActivity以使用Tabhost。在兒童活動中顯示DialogFragment Android
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 [email protected] 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。
在此先感謝。
更多的代碼顯示在您的活動對話框之前顯示在您的片段對話之前... .......... – 2013-07-30 05:37:28
你在主線程 – 2013-07-30 05:38:01