-3

因此,我按照在线教程介绍了在 Android Studio 中创建 TabbedLayout。一切顺利,我完成了 TabbedLayout。

在此处输入图像描述

以上是我的文件。每个选项卡都有一个 java 类和片段,当然还有 MainActivity。但我不知道在哪里为每个单独的片段或选项卡添加代码。我尝试将它添加到相应的 java 类中,但里面没有 onCreate 方法,所以我无法访问 findViewByID()。

下面是每个选项卡 java 类的样子。 在此处输入图像描述

我也尝试从 mainActivity(我可以访问 findViewByID)执行此操作,但我的应用程序一直强制关闭。

所以我只是不确定从这里去哪里。任何帮助,将不胜感激。谢谢!

4

1 回答 1

0

onViewCreated()在 onCreateView 之后使用

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_home, container, false);
}

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

    //here your code
    TextView text = view.findViewById(R.id.text);
}
于 2017-05-05T02:16:31.143 回答