2017-07-19 123 views
0

我有如下一個佈局文件:無法找到包括佈局佈局Android應用

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_below="@+id/gallery"> 

<RelativeLayout 
    android:id="@+id/topLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <include 
     android:id="@+id/tool_bar" 
     layout="@layout/tool_bar_activity" > 
    </include> 

    <include 
     android:id="@+id/gallery" 
     android:layout_below="@+id/tool_bar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     layout="@layout/create_profile_gallery" > 
    </include> 

    . 
    . 
    . 
</RelativeLayout> 

並呈現如下:

enter image description here

所包含的佈局, tool_bar_activity和create_profile_gallery分別如下:

<android.support.design.widget.AppBarLayout 
    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="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?attr/colorPrimary" 
     android:minHeight="?attr/actionBarSize" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:titleTextAppearance="@style/ToolbarTitle"> 

     . 
     . 
     . 
    </android.support.v7.widget.Toolbar> 
</android.support.design.widget.AppBarLayout> 

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

<LinearLayout 
    android:id="@+id/categories_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

. 
. 
. 
</LinearLayout> 
</HorizontalScrollView> 

我看到的問題是,findViewById返回null categories_layout但不爲空的工具欄,儘管兩者都包含在各自的(含)的佈局:

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

    // Get a pointer to the gallery layout. This is where gallery items will be inserted. 
    categories_layout = (LinearLayout) findViewById(R.id.categories_layout); 

爲什麼應該找到一個佈局而不是另一個佈局?

回答

0

categories_layout不能爲null,因爲在Activity中調用onCreate方法時,父佈局中的所有視圖都會加載到內存中。

+0

這就是我想,也是。 – FractalBob

0

我必須能夠訪問它的子元素之前膨脹的觀點:

      // First, inflate the view that contains the layout we want to insert into. 
         LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         // Get a pointer to the gallery layout. This is where gallery items will be inserted. 
         categories_layout = (LinearLayout) findViewById(R.id.categories_layout); 
         categories_layout.removeAllViews(); 
         for (int i = 0; i < catNameObj.length; i++) { 
          // Get the view for the gallery items. These will be inserted into categories_layout after the all_categories list 
          // has been downloaded from the server, in downloadFile(). 
          galleryView = vi.inflate(R.layout.category_list, null); 

          ImageView imgView = (ImageView) galleryView.findViewById(R.id.node); 
          . 
          . 
          .