0
我所有的活動都擴展了MainActivity並共享了activity_main佈局。在我的一項活動中,我使用的是滑動式庫,所以佈局是不同的。當我在添加布局視圖後嘗試附加片段時,出現錯誤:替換片段:IllegalArgumentException:未找到id的視圖
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(I標記有問題的線路):
@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>
SearchActivity延伸MainActivity它採用activity_main。在activity_main中是抽屜佈局。我將search_appbar_layout添加到該抽屜佈局 – Psest328