I have a Fragment
(it is not an inner class, and it does not have any constructor whatsoever)
public class PreferenceListFragment extends ListFragment implements OnClickListener
I'm getting this crash report on the Android Developer Console:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.redacted.redacted/com.redacted.redacted.PreferenceActivity}:
android.support.v4.app.Fragment$InstantiationException:
Unable to instantiate fragment com.redacted.redacted.PreferenceListFragment$3:
make sure class name exists, is public, and has an empty constructor that is
public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1750)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1766)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2960)
at android.app.ActivityThread.access$1600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:945)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3818)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.support.v4.app.Fragment$InstantiationException:
Unable to instantiate fragment com.redacted.redacted.PreferenceListFragment$3:
make sure class name exists, is public, and has an empty constructor that
is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:399)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1760)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:200)
at com.redacted.redacted.PreferenceActivity.onCreate(PreferenceActivity.java:37)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1710)
... 12 more
Caused by: java.lang.InstantiationException:
com.redacted.redacted.PreferenceListFragment$3
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1409)
at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
... 18 more
I am unable to replicate this on any of my test devices.
Here's the PreferenceActivity.onCreate
where the exception is occurring:
public class PreferenceActivity extends FragmentActivity{
PreferenceListFragment frag;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.preference);
frag = (PreferenceListFragment) getSupportFragmentManager().
findFragmentById(R.id.preference_list_frag);
frag.setState(AlarmPreferenceState.Selected);
frag.setIsTwoPane(false);
}
.
.
.
}
And here is R.layout.preference
:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="com.redacted.redacted.PreferenceListFragment"
android:id="@+id/preference_list_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Anyone know why I might be getting this exception, and/or how to reproduce it?
PreferenceListFragment
class have any other constructors besides a no arguments one? – ContemporaneousFragment
and someone on SO said not to have any constructor in aFragment
– SplenomegalyPreferenceListFragment
class an inner class in some other class? If yes then make itstatic
, otherwise you'll get that exception. – ContemporaneousPreferenceListFragment
and Android can't instantiate them. If this is true than you should avoid those anonymous classes. – ContemporaneousPreferenceListFragment
. Except for that I couldn't find anything wrong in your project. – Vermiculite