我找到了与我类似的问题的主题,但到目前为止找不到我正在寻找的答案。我的应用程序包含一个FragmentActivity
托管一个ViewPagerAdapter
(FragmentPagerAdapter 的子级),每个选项卡中都有一个片段。我的 ViewPagerAdapter 在父活动的 OnCreateView 函数中实例化
_adapter = new ViewPagerAdapter(getApplicationContext()
, getSupportFragmentManager()
, numOfTabs
, status);
ViewPagerAdapter 实现了所需的最少方法 getItem
,getCount
并且getItemPosition
我getItem
为每个位置初始化一个不同的片段:
@Override
public Fragment getItem(int position)
{
Fragment f = new Fragment();
Log.d("Adbox",String.format("Inside ViewPagerAdapter.getItem(%s)",position));
switch(position)
{
case 0:
Log.d("Adbox","All offers ==");
f=FragmentAllOffers.newInstance(_context);
f.setRetainInstance(true);
break;
case 1:
Log.d("Adbox","Nearby offers ==");
f=FragmentNearbyOffers.newInstance(_context);
//f.setRetainInstance(true);
break;
case 2:
Log.d("Adbox","My coupons ==");
f=FragmentCoupons.newInstance(_context);
f.setRetainInstance(true);
break;
case 3:
Log.d("Adbox","Account ==");
f=FragmentAccount.newInstance(_context);
f.setRetainInstance(true);
//f=LayoutLocal.newInstance(_context);
break;
case 4:
Log.d("Adbox","Preferences ==");
f=FragmentPreferences.newInstance(_context);
f.setRetainInstance(true);
break;
default:
break;
}
return f;
}
setRetainInstance(true)
在我努力解决我面临的问题时添加了调用,但也没有帮助。
最后,上面的每个片段都以应用程序上下文作为参数实现了一个公共静态 newInstance() 函数。例如 FragmentNearbyOffers 包含以下内容:
public static android.support.v4.app.Fragment newInstance(Context ctx)
{
FragmentNearbyOffers f = new FragmentNearbyOffers();
ctx = context;
//Bundle bdl = new Bundle();
return f;
}
一个更重要的信息是父活动被声明为 singleInstance 并且出于某些原因我想保持这种状态。
一切正常,但在某个时候,当活动在后台运行了一段时间,我尝试通过 TaskManager 或单击应用程序图标返回它时出现异常
android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.advisor.FragmentNearbyOffers$1: make sure class name exists, is public, and has an empty constructor that is public
类名肯定存在,它是公共的,并且没有构造函数,就像有一个空的构造函数一样。我什至明确添加了一个空构造函数,但这也无济于事,尽管我验证了它被调用了。
从我从这里的各种帖子中了解到,Android 在恢复应用程序时,会将未链接到原始活动的片段的新实例放入 FragmentPagerAdapter 中。我也验证了这一点,因为当从片段内部调用 getActivity 时,我接收null ..但我不明白为什么我得到这个异常,因为有一个空的构造函数......我什至不知道在哪里解决这个问题,因为执行进入活动的onCreate,然后立即进入片段的空构造函数,然后我得到异常..片段的任何其他方法,即onAttach,onCreate等根本不被调用..所以看起来它在构造片段时实际上崩溃了..
我附上了我得到的整个堆栈跟踪,以防万一它有帮助:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.advisor/com.advisor.AdBoxWidgetConfigurationFragment}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.advisor.FragmentNearbyOffers$1: make sure class name exists, is public, and has an empty constructor that is public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.advisor.FragmentNearbyOffers$1: 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.advisor.AdBoxWidgetConfigurationFragment.onCreate(AdBoxWidgetConfigurationFragment.java:60)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
... 11 more
Caused by: java.lang.InstantiationException: can't instantiate class com.advisor.FragmentNearbyOffers$1; no empty constructor
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.support.v4.app.Fragment.instantiate(Fragment.java:388)