替换片段:IllegalArgumentException:找不到id的视图

时间:2015-10-16 19:27:42

标签: android android-fragments

我的所有活动都扩展了MainActivity并共享了activity_main布局。在我的一个活动中,我正在使用slidinguppanel库,因此布局不同。当我在添加布局视图后尝试附加片段时,出现错误:

10-16 15:25:14.155 32590-32590/com.myproject.myapp E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject.myapp/com.myproject.myapp.search.SearchActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0d00ea (com.myproject.myapp/searchContainer) for fragment SearchFragment{2e02f51a #0 id=0x7f0d00ea SearchFragment}

我做错了什么?

MainActivity.java oncreate:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    if (getSupportActionBar() != null) {
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_hamburger_icon);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    mMainContainer = (FrameLayout) findViewById(R.id.mainContainer);
    mCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
    mAppBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    mUserImageView = (RoundedImageView) navigationView.findViewById(R.id.userImageView);
    mRealNameView = (TextView) navigationView.findViewById(R.id.realNameText);
    mUserNameView = (TextView) navigationView.findViewById(R.id.userNameText);

    showUserInfo(mUser != null);
}

activity_main.xml中:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/drawer_items" />

</android.support.v4.widget.DrawerLayout>

SearchActivity.java onCreate(我标记了有问题的行):

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    EventBus.getDefault().register(this);

    if (getSupportActionBar() != null) {
        getSupportActionBar().hide();
    }

    mDrawerLayout.removeView(mCoordinatorLayout);

    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.search_appbar_layout, mDrawerLayout, false);

    mSlidingPanel = (SlidingUpPanelLayout) view.findViewById(R.id.sliding_layout);
    Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);

    mDrawerLayout.addView(view);

    toolbar.setTitleTextColor(Color.WHITE);
    setSupportActionBar(toolbar);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            toggleDrawer();
        }
    });

    SearchFragment searchFragment = (SearchFragment) getSupportFragmentManager().findFragmentByTag("SearchFragment");
    if (searchFragment == null) {
        FrameLayout searchContainer = (FrameLayout) view.findViewById(R.id.searchContainer);
        searchContainer.setId(R.id.searchContainer);
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.searchContainer, SearchFragment.newInstance(), "SearchFragment") // here's the problem
                .commit();
    }

    viewPager = (ViewPager) view.findViewById(R.id.vp_searchResultViewPager);
    SearchResultPagerAdapter pagerAdapter = new SearchResultPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(pagerAdapter);
    TabLayout tabStrip = (TabLayout) view.findViewById(R.id.tl_searchResultTabs);
    tabStrip.setupWithViewPager(viewPager);
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_SETTLING) {
                switch (viewPager.getCurrentItem()) {
                    case 1:
                        if (foursquareResultsObject != null) {
                            EventBus.getDefault().post(new PopulateSearchMapEvent(foursquareResultsObject));
                        }
                        break;

                    default: //case 0
                        if (foursquareResultsObject != null) {
                            EventBus.getDefault().post(new PopulateSearchListEvent(foursquareResultsObject));
                        }
                        break;
                }
            }
        }
    });
}

search_appbar_layout.xml:

<?xml version="1.0" encoding="utf-8"?>

<com.sothree.slidinguppanel.SlidingUpPanelLayout xmlns:sothree="http://schemas.android.com/apk/res-auto"
    android:id="@+id/sliding_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    sothree:umanoDragView="@+id/toolbar"
    sothree:umanoInitialState="expanded"
    sothree:umanoOverlay="true"
    sothree:umanoPanelHeight="?attr/actionBarSize"
    sothree:umanoParallaxOffset="0dp"
    sothree:umanoShadowHeight="4dp">

    <!-- MAIN CONTENT -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:paddingBottom="?attr/actionBarSize">

        <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

                <android.support.design.widget.TabLayout
                    android:id="@+id/tl_searchResultTabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </android.support.design.widget.AppBarLayout>

            <android.support.v4.view.ViewPager
                android:id="@+id/vp_searchResultViewPager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
        </android.support.design.widget.CoordinatorLayout>
    </LinearLayout>

    <!-- SLIDING LAYOUT -->
    <LinearLayout
        android:id="@+id/ll_searchDragView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar xmlns:bar="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:gravity="end|center_vertical"
            android:paddingEnd="@dimen/activity_horizontal_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingStart="@dimen/activity_horizontal_margin"
            bar:navigationIcon="@drawable/ic_action_hamburger_icon"
            bar:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            bar:title="@string/search_venues"
            bar:titleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        </android.support.v7.widget.Toolbar>

        <FrameLayout
            android:id="@+id/searchContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>

1 个答案:

答案 0 :(得分:2)

SearchActivity中,您使用的是activity_main布局,而不是您拥有片段容器的search_app_bar_layout。

从此更改

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

到此:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_appbar_layout);