带有片段的Android InstantiationException(公开) [英] Android InstantiationException With Fragment (It Is Public)
问题描述
我有一个Fragment
(它不是一个内部类,并且没有任何构造函数)
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
我在Android开发者控制台上收到此崩溃报告:
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.
这是发生异常的PreferenceActivity.onCreate
:
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);
}
.
.
.
}
这是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?
推荐答案
您MUST
有一个空的公共构造函数.谁告诉您不要使用片段构造器,就会将您引向错误的方向.
You MUST
have an empty public constructor. Whoever told you to not have a fragment constructor steered you in the wrong direction.
他们可能告诉您的是,不要有一个接受参数的构造函数,因为系统在重新创建片段时可能不会调用这些参数.在这种情况下,请使用文档中的示例为片段提供参数.
What they might have told you, is to not have a constructor that accepts arguments, since those may not be called by the system when re-creating the fragments. In that case, use the example in the docs to supply arguments to your fragment.
引自文档:
Fragment的所有子类都必须包含一个公共的空构造函数.框架通常会在需要时重新实例化片段类,尤其是在状态还原期间,并且需要能够找到此构造函数以实例化它.如果空构造函数不可用,则在状态还原期间的某些情况下会发生运行时异常. [1],[2]
All subclasses of Fragment must include a public empty constructor. The framework will often re-instantiate a fragment class when needed, in particular during state restore, and needs to be able to find this constructor to instantiate it. If the empty constructor is not available, a runtime exception will occur in some cases during state restore. [1], [2]
[1]: https://developer.android.com/reference/android/app/Fragment .html
[2]: https://developer.android.com/reference/android/app/Fragment.html#Fragment()
这篇关于带有片段的Android InstantiationException(公开)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!