這是訪問谷歌的API片段中:訪問外部類的方法的靜態內部類
/* Called from ErrorDialogFragment when the dialog is dismissed. */
public void onDialogDismissed() {
mResolvingError = false;
}
/* A fragment to display an error dialog */
public static class ErrorDialogFragment extends DialogFragment {
public ErrorDialogFragment() { }
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the error code and retrieve the appropriate dialog
int errorCode = this.getArguments().getInt(DIALOG_ERROR);
return GooglePlayServicesUtil.getErrorDialog(errorCode,
this.getActivity(), REQUEST_RESOLVE_ERROR);
}
@Override
public void onDismiss(DialogInterface dialog) {
((MainActivity)getActivity()).onDialogDismissed();
}
}
雖然這工作,如果代碼是一個活動,你怎麼onDialogDismissed()
一個片段使用靜態片段類
我這個試了一下里面,似乎不工作:
@Override
public void onDismiss(DialogInterface dialog) {
ErrorDialogFragment innerClass = new OuterFragment.ErrorDialogFragment();
innerClass.onDialogDismissed(); // Still cant find the method of the outer class
}
現在我明白了爲什麼。我也用這個'((OuterClass)getParentFragment())。onDialogDismissed();'因爲它嵌套在一個片段中。 – Jan 2015-03-25 09:46:22