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

时间:2013-08-01 14:05:45

标签: android android-fragments android-button

我是Android开发的新手,我正在尝试编写一个简单的小应用程序。 这个应用程序除了从2个EditTexts中读取文本之外什么都不做,当用户按下“发送”按钮时,它应该将文本写入2个TextViews。这些TextView中的每一个都在一个片段中。

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

以下是相关的codeBlocks:

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

5 个答案:

答案 0 :(得分:7)

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

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

您正在sendTextButton内找到ID为headline_view.xml的按钮。但是从你发布的布局中,没有带有id sendTextButton的Button。那样你就得零了

答案 1 :(得分:1)

您的按钮位于activity_main.xml内,但您正在充气R.layout.headline_view

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

答案 2 :(得分: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是点击的视图,因此coure v.findViewById()不起作用..
为了实现你的目标,你可以声明你的EditText和TextView全局,在onCreate()中使用inflater实例化它们,你可以使用onClick方法!

答案 3 :(得分: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
}

答案 4 :(得分:0)

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