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
diff --git a/src/com/android/settings/ProxySelector.java b/src/com/android/settings/ProxySelector.java
index 774e47c..4bd4585 100644
--- a/src/com/android/settings/ProxySelector.java
+++ b/src/com/android/settings/ProxySelector.java
@@ -113,7 +113,7 @@
if (mDialogFragment != 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));
}
diff --git a/src/com/android/settings/SettingsPreferenceFragment.java b/src/com/android/settings/SettingsPreferenceFragment.java
index 87a3511..5cfe218 100644
--- a/src/com/android/settings/SettingsPreferenceFragment.java
+++ b/src/com/android/settings/SettingsPreferenceFragment.java
@@ -454,7 +454,7 @@
if (mDialogFragment != 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));
}
@@ -541,22 +541,26 @@
private DialogInterface.OnCancelListener mOnCancelListener;
private DialogInterface.OnDismissListener mOnDismissListener;
- public SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
- super(fragment, dialogId);
+ public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) {
if (!(fragment instanceof Fragment)) {
throw new IllegalArgumentException("fragment argument must be an instance of "
+ Fragment.class.getName());
}
- mParentFragment = (Fragment) fragment;
- }
+ final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment();
+ settingsDialogFragment.setParentFragment(fragment);
+ settingsDialogFragment.setDialogId(dialogId);
+
+ return settingsDialogFragment;
+ }
@Override
public int getMetricsCategory() {
- if (mDialogCreatable == null) {
+ if (mParentFragment == null) {
return Instrumentable.METRICS_CATEGORY_UNKNOWN;
}
- final int metricsCategory = mDialogCreatable.getDialogMetricsCategory(mDialogId);
+ final int metricsCategory =
+ ((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId);
if (metricsCategory <= 0) {
throw new IllegalStateException("Dialog must provide a metrics category");
}
@@ -639,6 +643,14 @@
}
}
}
+
+ private void setParentFragment(DialogCreatable fragment) {
+ mParentFragment = (Fragment) fragment;
+ }
+
+ private void setDialogId(int dialogId) {
+ mDialogId = dialogId;
+ }
}
protected boolean hasNextButton() {
diff --git a/tests/robotests/src/com/android/settings/SettingsDialogFragmentTest.java b/tests/robotests/src/com/android/settings/SettingsDialogFragmentTest.java
index c78a44a..ed0e5a0 100644
--- a/tests/robotests/src/com/android/settings/SettingsDialogFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/SettingsDialogFragmentTest.java
@@ -51,8 +51,8 @@
public void testGetMetrics_shouldGetMetricFromDialogCreatable() {
when(mDialogCreatable.getDialogMetricsCategory(DIALOG_ID)).thenReturn(1);
- mDialogFragment =
- new SettingsPreferenceFragment.SettingsDialogFragment(mDialogCreatable, DIALOG_ID);
+ mDialogFragment = SettingsPreferenceFragment.SettingsDialogFragment.newInstance(
+ mDialogCreatable, DIALOG_ID);
mDialogFragment.onAttach(RuntimeEnvironment.application);
mDialogFragment.getMetricsCategory();
@@ -65,8 +65,8 @@
when(mDialogCreatable.getDialogMetricsCategory(DIALOG_ID)).thenReturn(-1);
try {
- mDialogFragment =
- new SettingsPreferenceFragment.SettingsDialogFragment(mDialogCreatable, DIALOG_ID);
+ mDialogFragment = SettingsPreferenceFragment.SettingsDialogFragment.newInstance(
+ mDialogCreatable, DIALOG_ID);
mDialogFragment.onAttach(RuntimeEnvironment.application);
fail("Should fail with IllegalStateException");
} catch (IllegalStateException e) {