5

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

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

以下是相关的代码块:

片段的 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"
/>
4

5 回答 5

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

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

您正在寻找一个带有 id 的sendTextButtonButton headline_view.xml。但是从您发布的布局中,没有 ID 为 sendTextButton 的按钮。这样你就得到了 null

于 2013-08-01T14:09:47.040 回答
1

你的按钮在里面activity_main.xml,但你正在充气R.layout.headline_view

this.inflatedView = inflater.inflate(R.layout.headline_view, container, false);
于 2013-08-01T14:11:22.983 回答
1

那就是问题所在

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);
    }
});

View v 是单击的视图,所以当然 v.findViewById() 不起作用..
要实现您的目标,您可以声明您的 EditText 和 TextView 全局,在 onCreate() 中使用充气器来实例化它们,您可以使用 onClick 方法使用它们!

于 2013-08-01T14:11:30.653 回答
1

第一:膨胀视图

然后:行动!- >访问按钮或其他

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val view = inflater.inflate(R.layout.notifications_fragment, container, false)
    view?.findViewById<View>(R.id.Button)?.setOnClickListener {
        println("findViewById")

    }
    return view
}
于 2021-05-18T11:38:05.717 回答
0

如果 Button 不在您的片段正在膨胀的布局中,您必须设置您onClickListener从中管理片段的 Activity 内部而不是片段内部。

于 2013-08-01T14:12:09.110 回答