gpt4 book ai didi

java - getActivity().findViewById(int id) 即使在 fragment 完成构建后也会返回 null

转载 作者:行者123 更新时间:2023-12-01 21:14:31 25 4
gpt4 key购买 nike

我正在关注android's website中的android培训。我被困在 fragment 中的某个时刻。问题是我无法在 fragment 中获取对我的 TextView 的引用。

TextView view = (TextView) getActivity().findViewById(R.id.article_s);

我在一项 Activity 中有两个 fragment 。一个是简单的 ListFragment,另一个是简单的 TextView。这是 fragment 和 Activity 布局 xml 文件。

文章 fragment :

<!-- This is fragment_article.xml -->
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Text"
android:textSize="18sp"
android:id="@+id/article_s"/>

MainActivity(我的所有 fragment 的容器)

<!-- This is activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:layout_height="match_parent"
android:name="com.example.bora.fragmentsmyway.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp" />

<fragment
android:layout_height="match_parent"
android:name="com.example.bora.fragmentsmyway.ArticleFragment"
android:layout_weight="2"
android:id="@+id/article_fragment"
android:layout_width="0dp" />

</LinearLayout>

我的ArticleFragment中有一个公共(public)方法,它需要一个字符串来更新本身的textview的内容:

public void updateView(String article) {
TextView view = (TextView) getActivity().findViewById(R.id.article_s);
try{
view.setText(article);
} catch (NullPointerException e) {
e.printStackTrace();
}
}

创建所有 View 后,我从 MainActivity 中调用 updateView 方法。但当我尝试查找文章 TextView 时,出现 NullPointerException。我尝试过清理、更改 TextView 的资源 id。

但是我已经找到了解决这个问题的方法。我不知道为什么,但当我在 fragment_article.xml 中用 LinearLayout 包装 TextView 时,它似乎起作用了:

<LinearLayout
xmlns:android="..."
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Text"
android:textSize="18sp"
android:id="@+id/article_s" />

</LinearLayout>

但我想知道:在教程示例中,上面的代码如何工作,而当我编写时却无法工作?我逐字检查了每个代码,但我的和他们的没有区别。是因为版本的原因吗?

我正在使用 API 23,即 marshmallow。

最佳答案

在调用updateView之前,确保您的 fragment 已附加到 Activity

这个

 TextView view = (TextView) getActivity().findViewById(R.id.article_s);

会给你NPE,因为它不属于该 Activity

View 属于 fragment ,在这种情况下您会这样做

 View view = inflater.inflate(R.layout.fragment_layout, container, false);
TextView textView = (TextView) view.findViewById(R.id.article_s);

具体来说,fragment 可以通过 getActivity() 访问 Activity 实例,并轻松执行任务,例如在 Activity 布局中查找 View :

阅读与 Activity 通信@ https://developer.android.com/guide/components/fragments.html

因此,您使用 getActivity 来查找属于 Activity 的 View ,但您的代码中并非如此。

编辑 -

LinearLayout包装它应该不会有任何区别,因为它只是一个 View 组。如果您只需要一个 textview,则不需要 LinearLayout

阅读已接受的答案:Difference between getActivity() and view in Fragment

关于java - getActivity().findViewById(int id) 即使在 fragment 完成构建后也会返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40483580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com