view.findViewById尝试从片段布局引用元素时返回null

问题描述 投票:0回答:1

我有这个片段代码:

public class FirstFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false);
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText = (EditText)view.findViewById(R.id.editText);
                String text = editText.getText().toString();
                Bundle bundle = new Bundle();
                bundle.putString("text", text);
                NavHostFragment.findNavController(FirstFragment.this).navigate(R.id.first_to_second, bundle);
            }
        });
    }
}

其中view.findViewById(...)返回空值。该片段的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_first"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FirstFragment">

    <TextView
        android:id="@+id/textview_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="55dp"
        android:layout_marginLeft="55dp"
        android:layout_marginTop="258dp"
        android:text="@string/first_fragment_label"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginBottom="324dp"
        android:text="@string/next"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.458"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="232dp"
        android:layout_marginEnd="26dp"
        android:layout_marginRight="26dp"
        android:ems="10"
        android:hint="@string/edit_text_hints"
        android:importantForAutofill="no"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

有人可以看到这里有什么问题吗?

android android-layout android-fragments findviewbyid
1个回答
0
投票

您应该onCreateView返回视图:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
     savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_first, container, false);

    return view;
}
© www.soinside.com 2019 - 2024. All rights reserved.