我有一段代码,以前工作得很好。 然后它突然开始抛出这个错误:
07-29 14:20:45.160: E/crash(28862): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-29 14:20:45.160: E/crash(28862): at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
07-29 14:20:45.160: E/crash(28862): at android.view.ViewGroup.addView(ViewGroup.java:3415)
07-29 14:20:45.160: E/crash(28862): at android.view.ViewGroup.addView(ViewGroup.java:3360)
07-29 14:20:45.160: E/crash(28862): at android.view.ViewGroup.addView(ViewGroup.java:3336)
07-29 14:20:45.160: E/crash(28862): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
07-29 14:20:45.160: E/crash(28862): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:946)
07-29 14:20:45.160: E/crash(28862): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
07-29 14:20:45.160: E/crash(28862): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-29 14:20:45.160: E/crash(28862): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
07-29 14:20:45.160: E/crash(28862): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
07-29 14:20:45.160: E/crash(28862): at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
07-29 14:20:45.160: E/crash(28862): at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
07-29 14:20:45.160: E/crash(28862): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:555)
07-29 14:20:45.160: E/crash(28862): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
07-29 14:20:45.160: E/crash(28862): at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:913)
07-29 14:20:45.160: E/crash(28862): at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2824)
07-29 14:20:45.160: E/crash(28862): at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
07-29 14:20:45.160: E/crash(28862): at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
07-29 14:20:45.160: E/crash(28862): at com.werbuy.werbuy.activities.PurchaseActivity.onLanguageChanged(PurchaseActivity.java:677)
07-29 14:20:45.160: E/crash(28862): at com.werbuy.werbuy.fragments.SettingsFragment$7.onItemClick(SettingsFragment.java:405)
07-29 14:20:45.160: E/crash(28862): at com.quickaction.QuickAction$2.onItemClick(QuickAction.java:184)
07-29 14:20:45.160: E/crash(28862): at android.widget.AdapterView.performItemClick(AdapterView.java:299)
07-29 14:20:45.160: E/crash(28862): at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
07-29 14:20:45.160: E/crash(28862): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
07-29 14:20:45.160: E/crash(28862): at android.widget.AbsListView$3.run(AbsListView.java:3645)
07-29 14:20:45.160: E/crash(28862): at android.os.Handler.handleCallback(Handler.java:733)
07-29 14:20:45.160: E/crash(28862): at android.os.Handler.dispatchMessage(Handler.java:95)
07-29 14:20:45.160: E/crash(28862): at android.os.Looper.loop(Looper.java:136)
07-29 14:20:45.160: E/crash(28862): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-29 14:20:45.160: E/crash(28862): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 14:20:45.160: E/crash(28862): at java.lang.reflect.Method.invoke(Method.java:515)
07-29 14:20:45.160: E/crash(28862): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-29 14:20:45.160: E/crash(28862): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-29 14:20:45.160: E/crash(28862): at dalvik.system.NativeStart.main(Native Method)
当我打电话
时,这会下降fragmentAdapter.notifiyDataSetChanged(); //<this call used to work just fine
我的所有片段onCreateView
都是这样的
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_settings, null);
}
我如何知道导致问题的视图?
编辑寻呼机适配器
public class PurchaseFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragments;
@Inject
public PurchaseFragmentStatePagerAdapter(FragmentManager fm) {
super(fm);
}
public void setFragments(List<Fragment> fragments) {
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments == null ? 0 : fragments.size();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
public boolean setFragment(Fragment fragment, int position, boolean update) {
if(fragment == null) {
return false;
}
fragments.set(position, fragment);
if(update) {
notifyDataSetChanged();
}
return true;
}
public boolean removeFragmentAtIndex(int position, boolean update) {
if (getCount()<position -1) {
return false;
}
fragments.remove(position);
if(update) {
notifyDataSetChanged();
}
return true;
}
}
答案 0 :(得分:0)
问题是一个特殊的片段 由谷歌地图提供的SupportMapFragment,这是我的一个viewpager片段。 不幸的是,它嵌入在我加载的xml中。一旦我开始动态加载它,问题就消失了