Fix InstantiationException on fragment

Since we overrided the empty constructor,
it can't find a empty constructor when
Fragment tries to instantiate a fragment
in framework.

So, we can't override this constructor,
and then just use newInstance() to initialize
and setup a new Fragment.

Test: robotest
Change-Id: Ifcd1c1771bc69d947caeee5c5bc055c4f94365c2
Fixes: 115676209
This commit is contained in:
tmfang
2018-10-05 18:45:07 +08:00
parent b402a56fec
commit d5405cf49d
3 changed files with 24 additions and 12 deletions

View File

@@ -113,7 +113,7 @@ public class ProxySelector extends InstrumentedFragment implements DialogCreatab
if (mDialogFragment != null) { if (mDialogFragment != null) {
Log.e(TAG, "Old dialog fragment not null!"); Log.e(TAG, "Old dialog fragment not null!");
} }
mDialogFragment = new SettingsDialogFragment(this, dialogId); mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId);
mDialogFragment.show(getActivity().getSupportFragmentManager(), Integer.toString(dialogId)); mDialogFragment.show(getActivity().getSupportFragmentManager(), Integer.toString(dialogId));
} }

View File

@@ -454,7 +454,7 @@ public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceF
if (mDialogFragment != null) { if (mDialogFragment != null) {
Log.e(TAG, "Old dialog fragment not null!"); Log.e(TAG, "Old dialog fragment not null!");
} }
mDialogFragment = new SettingsDialogFragment(this, dialogId); mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId);
mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId)); mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId));
} }
@@ -541,22 +541,26 @@ public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceF
private DialogInterface.OnCancelListener mOnCancelListener; private DialogInterface.OnCancelListener mOnCancelListener;
private DialogInterface.OnDismissListener mOnDismissListener; private DialogInterface.OnDismissListener mOnDismissListener;
public SettingsDialogFragment(DialogCreatable fragment, int dialogId) { public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) {
super(fragment, dialogId);
if (!(fragment instanceof Fragment)) { if (!(fragment instanceof Fragment)) {
throw new IllegalArgumentException("fragment argument must be an instance of " throw new IllegalArgumentException("fragment argument must be an instance of "
+ Fragment.class.getName()); + Fragment.class.getName());
} }
mParentFragment = (Fragment) fragment;
}
final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment();
settingsDialogFragment.setParentFragment(fragment);
settingsDialogFragment.setDialogId(dialogId);
return settingsDialogFragment;
}
@Override @Override
public int getMetricsCategory() { public int getMetricsCategory() {
if (mDialogCreatable == null) { if (mParentFragment == null) {
return Instrumentable.METRICS_CATEGORY_UNKNOWN; return Instrumentable.METRICS_CATEGORY_UNKNOWN;
} }
final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId); final int metricsCategory =
((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId);
if (metricsCategory <= 0) { if (metricsCategory <= 0) {
throw new IllegalStateException("Dialog must provide a metrics category"); throw new IllegalStateException("Dialog must provide a metrics category");
} }
@@ -639,6 +643,14 @@ public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceF
} }
} }
} }
private void setParentFragment(DialogCreatable fragment) {
mParentFragment = (Fragment) fragment;
}
private void setDialogId(int dialogId) {
mDialogId = dialogId;
}
} }
protected boolean hasNextButton() { protected boolean hasNextButton() {

View File

@@ -51,8 +51,8 @@ public class SettingsDialogFragmentTest {
public void testGetMetrics_shouldGetMetricFromDialogCreatable() { public void testGetMetrics_shouldGetMetricFromDialogCreatable() {
when(mDialogCreatable.getDialogMetricsCategory(DIALOG_ID)).thenReturn(1); when(mDialogCreatable.getDialogMetricsCategory(DIALOG_ID)).thenReturn(1);
mDialogFragment = mDialogFragment = SettingsPreferenceFragment.SettingsDialogFragment.newInstance(
new SettingsPreferenceFragment.SettingsDialogFragment(mDialogCreatable, DIALOG_ID); mDialogCreatable, DIALOG_ID);
mDialogFragment.onAttach(RuntimeEnvironment.application); mDialogFragment.onAttach(RuntimeEnvironment.application);
mDialogFragment.getMetricsCategory(); mDialogFragment.getMetricsCategory();
@@ -65,8 +65,8 @@ public class SettingsDialogFragmentTest {
when(mDialogCreatable.getDialogMetricsCategory(DIALOG_ID)).thenReturn(-1); when(mDialogCreatable.getDialogMetricsCategory(DIALOG_ID)).thenReturn(-1);
try { try {
mDialogFragment = mDialogFragment = SettingsPreferenceFragment.SettingsDialogFragment.newInstance(
new SettingsPreferenceFragment.SettingsDialogFragment(mDialogCreatable, DIALOG_ID); mDialogCreatable, DIALOG_ID);
mDialogFragment.onAttach(RuntimeEnvironment.application); mDialogFragment.onAttach(RuntimeEnvironment.application);
fail("Should fail with IllegalStateException"); fail("Should fail with IllegalStateException");
} catch (IllegalStateException e) { } catch (IllegalStateException e) {