android - 在 Fragment 中调用 findViewById 查找按钮返回 null

标签 android android-fragments android-button

我是 android 开发的新手,我正在尝试编写一个小而简单的应用程序。 这个应用程序应该只从 2 个 EditTexts 读取文本,当用户按下“发送”按钮时,它应该将文本写入 2 个 TextViews。这些 TextView 中的每一个都在一个 Fragment 中。

但是我无法将 onClickEvent 添加到 Button,因为 findViewById(R.id.sendTextButton) 总是返回 null。我试图让这个应用程序正常工作,但我现在找不到任何解决方案。

这里是相关的代码块:

fragment 的 onCreateView 函数,应该处理第一个输入(HeadlineFragment.java):

Button sendButton = null;
View inflatedView = null;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    this.inflatedView = inflater.inflate(R.layout.headline_view, container, false);

    sendButton = (Button) inflatedView.findViewById(R.id.sendTextButton);

    if(sendButton == null)
    {
        Log.d("debugCheck", "HeadFrag: sendButton is null");
        return inflatedView;
    }
    sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String headline = ((EditText) v.findViewById(R.id.enterHeadlineText)).getText().toString();
            ((TextView) v.findViewById(R.layout.headline_view)).setText(headline);
        }
    });
    return inflatedView;
}

带有布局的 xml 文件 (activity_main.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:id="@+id/enterHeadlineText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/enterHeadlineTextHint"
            android:maxLines="1"
            android:inputType="textCapSentences"
        />

        <EditText
            android:id="@+id/enterMessageText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:hint="@string/enterMessageTextHint"
            android:maxLines="1"
            android:inputType="textCapSentences"
            />

        <Button
            android:id="@+id/sendTextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="@string/sendTextButton"
            />



        <fragment android:name="com.example.mysecondapplication.HeadlineFragment"
            android:id="@+id/headlineFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            />

        <fragment android:name="com.example.mysecondapplication.MessageFragment"
            android:id="@+id/messageFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            />



</LinearLayout>

以及带有 TextView 的 xml 文件,其中应包含文本 (headline_view.xml):

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/headline_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:text="@string/hello_world"
/>

最佳答案

this.inflatedView = inflater.inflate(R.layout.headline_view, container, false);

sendButton = (Button) inflatedView.findViewById(R.id.sendTextButton);

您正在 headline_view.xml 中寻找 ID 为 sendTextButton 的按钮。但是从您发布的布局来看,没有 ID 为 sendTextButton 的按钮。这就是你得到 null 的方式

关于android - 在 Fragment 中调用 findViewById 查找按钮返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17996339/

相关文章:

java - 如何将 AppCompatActivity 更改为 Fragment

android - fragment 选项卡中的 fragment

android - 弹出后堆栈后,我的 fragment 没有被垃圾收集

android - 在操作栏的右上角添加一个按钮

android - 如何为所有 Activity 创建通用的 Android XML 布局

android - '缺少类型或命名空间名称'

android - 为什么与 ui 线程的通信只能通过处理程序?

android -/hardware/qcom/media-caf/msm8916 出现错误

android - 从线性布局以编程方式设置两个按钮之间的边距

Android Jetpack 组合 IconButton 填充