“选项卡布局”活动出现PlaceholderFragment错误

时间:2019-05-02 04:35:02

标签: android android-tabs

这是我第一次使用“标签活动”。我有一个现有项目,其中包含: 1.主要活动 2.导航抽屉活动

现在,我添加了一个选项卡活动,但它似乎与导航抽屉非常不同。我想做几件事:

  1. 在MainActivity上添加一个按钮,然后启动选项卡活动。

    这使我出错。

      

    java.lang.NullPointerException:尝试在null对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'   com.example.learnfragment.ui.main.PlaceholderFragment $ 1.onChanged(PlaceholderFragment.java:53)   com.example.learnfragment.ui.main.PlaceholderFragment $ 1.onChanged(PlaceholderFragment.java:50)

    这对我来说很奇怪,因为当我使用“导航活动”时,我从未使用过占位符,即使我从中学到的教程视频中都使用了占位符。导航抽屉中的所有OnClick动作,都是在“导航活动”中编写的。 为什么标签活动需要占位符??它的功能是什么,我可以删除它吗?为什么本教程使用占位符,但是我尝试将onClicklistenr放入仍可以正常工作的Fragment中?

  2. 在View_pager上添加1个按钮

    我尝试在Tab_activity.xml中的View_pager上拖动一个按钮,但该按钮没有显示在选项卡活动中。

tab_activity.xml

<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=".TabActivity">

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

           <--Add a Button but it does not show up on the activity-->
        <Button
            android:text="test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </android.support.v4.view.ViewPager>

PlaceHolder.java

    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_fragment_main, container, false);
        final TextView textView = root.findViewById(R.id.section_label);

// below is Line 50
        pageViewModel.getText().observe(this, new Observer<String>() {
            @Override
            // Line 53
            // There is no textview in my activity_tab.xml
            // Why AS would automatically create this?
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        return root;

教程视频显示,他可以将按钮拖动到ViewPager,但我不能。什么

0 个答案:

没有答案
相关问题