片段未创建其对应的视图

时间:2019-06-26 15:51:59

标签: android android-layout android-fragments android-tablayout

我有一个带有2个标签的TabLayout。对于每个选项卡,我创建了相应的片段。

活动类(扩展AppCompatActivity)是这个(ToolsActivity.java):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tools);
    SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
    mViewPager = findViewById(R.id.view_pager);
    mViewPager.setAdapter(sectionsPagerAdapter);
    TabLayout tabs = findViewById(R.id.tabs);
    tabs.setupWithViewPager(mViewPager);

    tabs.getTabAt(0).setIcon(R.drawable.ic_tab_reading);
    tabs.getTabAt(1).setIcon(R.drawable.ic_tab_writing);

    tabs.setTabMode(TabLayout.MODE_SCROLLABLE);

    mCurrentSelectedListener = new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            mViewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            // ...
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            // ...
        }
    };

    tabs.addOnTabSelectedListener(mCurrentSelectedListener);
}

对应的布局是这样的(activity_tools.xml):

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ToolsActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minHeight="?actionBarSize"
            android:padding="@dimen/appbar_padding"
            android:text="@string/title_activity_tools"
            android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:tabInlineLabel="true" />
    </android.support.design.widget.AppBarLayout>

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

寻呼机适配器包含一个Fragment对象数组。

    mLstFragments.add(new UHFReadFragment());
    mLstFragments.add(new UHFWriteFragment());

该适配器也包含以下代码:

@Override
public Fragment getItem(int position) {
    if (mLstFragments.size() > 0) {
        return mLstFragments.get(position);
    }
    throw new IllegalStateException("No fragment at position " + position);
}

第一个选项卡是UHFReadFragment。这是其代码的一部分:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mContext = (ToolsActivity) getActivity();
    mSound = new Sound(mContext);

    mTagList = new ArrayList<HashMap<String, String>>();

    mAdapter = new SimpleAdapter(mContext, mTagList, R.layout.listtag_items,
            new String[]{"tagUii", "tagLen", "tagCount", "tagRssi"},
            new int[]{R.id.TvTagUii, R.id.TvTagLen, R.id.TvTagCount,
                    R.id.TvTagRssi});

    mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String result = msg.obj + "";
            String[] strs = result.split("@");
            addEPCToList(strs[0], strs[1]);
            mSound.playSound(1);
        }
    };

    mUHF = new UHF(mContext, mHandler);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mTvCount = (TextView) getView().findViewById(R.id.tv_count);
    mLvTags = (ListView) getView().findViewById(R.id.LvTags);
    mLvTags.setAdapter(mAdapter);
}

最后,这是第一个片段(uhf_read_fragment.xml)的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    tools:context=".ui.main.UHFReadFragment">

    <LinearLayout
        android:id="@+id/layout4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:background="@color/white"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp" >

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/tvTagUii"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/tv_count"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:text="0"
            android:textColor="@color/red"
            android:textSize="16sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/tvTagLen"
            android:visibility="gone" />

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/tvTagCount"
            android:textSize="15sp" />

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="RSSI"
            android:textSize="15sp"/>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/gray" />

    <ListView
        android:id="@+id/LvTags"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

问题在于片段的布局没有出现,并且实际上从未调用过onViewCreated。

还缺少什么?我认为通过使用“ tools:context =”。ui.main.UHFReadFragment“”,布局将与控制Fragment的类相关联。

仅显示活动的布局(运行应用程序时,仅显示标题和标签页眉)。

问候 海梅

1 个答案:

答案 0 :(得分:0)

您需要在Fragment类(UHFReadFragment)中使用onCreateView方法来扩大布局

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
  // Inflate the layout for this fragment
  return inflater.inflate(R.layout.uhf_read_fragment, container, false);
}

希望这可以解决您的问题