管理选项卡片段内的文本视图和开关。

huangapple 未分类评论45阅读模式
英文:

Manage textview and switches inside tab fragments

问题

所以,各位,我的问题很简单,我正在将我的应用程序布局迁移到 Android 的标签视图(tabview)。

我已经将所有开关和文本视图的代码放在了 activity_main.xml 布局文件中。

现在,我创建了3个布局片段(layout fragments)的资源 XML 文件,并将所有内容迁移到了那里,此外,我还在 activity_main 文件中包含了布局应用栏(app bar)。

我没有改动任何东西,将布局元素的所有 Java 代码都留在了 MainActivity.java 中。

现在,应用程序崩溃了,因为在片段布局文件中声明的所有布局元素返回为 null。

我是否需要将布局元素的代码设置在各个片段关联的 Java 文件中呢?

谢谢

英文:

So guys my question is simple, i'm migrating my app layout to android tabview.

I had all the code of the switches and textviews inside my activity_main.xml layout file.

Now i made 3 layout fragments resource xml files and i migrated everything there, plus i included the layout app bar in the activity_amin file

I left all the java codes of the layout elements inside MainActivity.java without changing anything.

Now the app crashes becouse all the layout elements declared inside the fragment layout files return null.

Do i have to set the layout elemnts codes inside the frgament java associated files?

Thank you

答案1

得分: 0

你应该绝对使用 Fragment.java 类来处理片段的 GUI 组件。
如果你需要关于如何使用片段的刷新,可以阅读这里的 codelab:https://codelabs.developers.google.com/codelabs/advanced-android-training-fragments/index.html?index=..%2F..index#0

最好的祝愿,
Sebi

英文:

You should definitely use the Fragment.java-classes for addressing the gui-components of a fragment.
If you need a refresh for how to work with fragments you can read thru the codelab here: https://codelabs.developers.google.com/codelabs/advanced-android-training-fragments/index.html?index=..%2F..index#0

Best
Sebi

答案2

得分: 0

这很简单,你可能是在片段的onCreateView内部工作。

尝试在onActivityCreated内部工作,因为在onCreateView中视图尚未准备好,所有内容都将返回null,所以如果你在以下部分工作:

Kotlin:

override fun onActivityCreated

Java:

@Override
    public void onActivityCreated

这种情况就不会发生。

英文:

That's simple you probably working inside onCreateView of the fragment

try to work inside the onActivityCreate, because onCreateView the view is not ready and every thing will return null, so if you work on :

Kotlin:

override fun onActivityCreated

Java :

@Override
    public void onActivityCreated

that dosen't happen.

huangapple
  • 本文由 发表于 2020年4月10日 00:52:33
  • 转载请务必保留本文链接:https://java.coder-hub.com/61126229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定