[英]java.lang.IllegalStateException activity with viewPager and tabs
When I go back from the next activity, always savedInstanceState is null and when I rotate screen or turn back , I get this error: java.lang.IllegalStateException: The specified child already has a parent. 当我从下一个活动返回时,始终saveInstanceState为null,并且在旋转屏幕或返回时,出现以下错误:java.lang.IllegalStateException:指定的孩子已经有一个父母。 You must call removeView() on the child's parent first. 您必须先在孩子的父母上调用removeView()。 I have tried to use in MyFragment View fragment = (View) inflater.inflate(R.layout.recycler_view, null); 我试图在MyFragment View中使用fragment =(View)inflater.inflate(R.layout.recycler_view,null); Or View fragment = (View) inflater.inflate(R.layout.recycler_view, container, false); 或者View片段=(View)inflater.inflate(R.layout.recycler_view,container,false); or View fragment = (View) inflater.inflate(R.layout.recycler_view, container); 或视图片段=(视图)inflater.inflate(R.layout.recycler_view,container); but i get the same error. 但我得到同样的错误。
Error log: 错误日志:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
at android.view.ViewGroup.addView(ViewGroup.java:3415)
at android.support.v4.view.ViewPager.addView(ViewPager.java:1342)
at android.view.ViewGroup.addView(ViewGroup.java:3360)
at android.view.ViewGroup.addView(ViewGroup.java:3336)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1094)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1624)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:447)
....
MainActivity 主要活动
public class MainActivity extends AppCompatActivity {
static public DrawerLayout mDrawerLayout;
private ArrayList<ArrayList<Article>> articlesHomeList;
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.d("--MainActivity", "onSaveInstanceState");
// Save the user's current game state
savedInstanceState.putSerializable("articlesHomeList", articlesHomeList);
savedInstanceState.putString("test", "teststetststs");
// Always call the superclass so it can save the view hierarchy state
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) {
Log.d("--MainActivity", "onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState, persistentState);
this.articlesHomeList = (ArrayList<ArrayList<Article>>) savedInstanceState.getSerializable("articlesHomeList");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = getIntent().getExtras();
articlesHomeList = (ArrayList<ArrayList<Article>>) bundle.getSerializable("articlesHomeList");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(R.string.app_name);
setSupportActionBar(toolbar);
// Setting ViewPager for each Tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
// Set Tabs inside Toolbar
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
Bundle bundleAll = new Bundle();
bundleAll.putSerializable("articles", (Serializable) this.articlesHomeList.get(2));
myFragment MyFragment = new MyFragment();
myFragment.setArguments(bundleAll);
AdapterTabs adapter = new AdapterTabs(getSupportFragmentManager());
adapter.addFragment(myFragment, "All");
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Log.e("Item selected", Integer.toString(id));
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} else if (id == android.R.id.home) {
mDrawerLayout.openDrawer(GravityCompat.START);
}
return super.onOptionsItemSelected(item);
}
myFragment: myFragment:
public class MyFragment extends Fragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragment = (View) inflater.inflate(R.layout.recycler_view, container, false);
recyclerView = (RecyclerView) fragment.findViewById(R.id.my_recycler_view);
articlesList = (List<Article>) this.getArguments().getSerializable("articles");
ContentAdapter adapter = new ContentAdapter(articlesList);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return recyclerView;
}
...
}
EDIT 编辑
I have added to MainActivity: 我已经添加了MainActivity:
if (savedInstanceState != null) {
this.articlesHomeList = (ArrayList<ArrayList<Article>>) savedInstanceState.getSerializable("articlesHomeList");
this.renderTabs(viewPager);
} else if (getIntent().getExtras() != null) {
Bundle bundle = getIntent().getExtras();
articlesHomeList = (ArrayList<ArrayList<Article>>) bundle.getSerializable("articlesHomeList");
this.renderTabs(viewPager);
} else {
this.loadArticlesHome(viewPager);
}
But always savedInstanceState is null. 但是始终saveInstanceState为null。
In the onCreateView
method of your MyFragment
fragment, change 在您的MyFragment
片段的onCreateView
方法中,更改
return recyclerView;
to 至
return fragment;
You should return the fragment
's view container, not the RecyclerView
's. 您应该返回fragment
的视图容器,而不是RecyclerView
的视图容器。 You see : the onCreateView
method is called to have the fragment instantiate its user interface view , not just a single component of it (in your case the RecyclerView
). 您将看到:调用了onCreateView
方法以使片段实例化其用户界面视图 ,而不仅仅是实例的单个组件(在您的情况下为RecyclerView
)。
EDIT: 编辑:
As @BlackBelt said, the app crashes because the RecyclerView
has already a parent, defined in the layout
, and it can't have two (the one in the layout and the ViewPager
) : which is clearly what the exception says 正如@BlackBelt所说,该应用程序崩溃是因为RecyclerView
已经在layout
定义了一个父项,并且它不能有两个(布局中的一个和ViewPager
):显然这是异常的意思
java.lang.IllegalStateException: The specified child already has a parent java.lang.IllegalStateException:指定的子代已经有一个父代
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.