Android 片段中findViewById上的空对象引用

Android 片段中findViewById上的空对象引用,android,Android,我目前正在尝试将我的xml文本视图链接到我的配置文件片段。但是,我在该行中得到一个空对象引用错误 nameAge = getView().findViewById(R.id.nameAge); 下面是Fragment.java package com.example.helloworld2; import android.view.LayoutInflater; import android.view.ViewGroup; import android.view.View; import a

我目前正在尝试将我的xml文本视图链接到我的配置文件片段。但是,我在该行中得到一个空对象引用错误

nameAge = getView().findViewById(R.id.nameAge);
下面是Fragment.java

package com.example.helloworld2;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View;
import android.os.Bundle;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

public class ProfileFragment extends Fragment{
    private TextView nameAge;
    private TextView occupation;
    private TextView description;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_profile, null);

        nameAge = getView().findViewById(R.id.nameAge);
        occupation = getView().findViewById(R.id.occupation);
        description = getView().findViewById(R.id.description);

        return view;
    }
}

Here is the xml file

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

    <ImageView
        android:id="@+id/profile_image"
        android:layout_width="300dp"
        android:layout_height="350dp"
        android:layout_gravity="center"
        android:src="@drawable/nero" />

    <TextView
        android:id="@+id/nameAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="50dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/occupation"
        android:textSize="40dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/description"
        android:paddingTop="40dp"
        android:textSize="30dp"/>

</LinearLayout>

谢谢

在OnView中执行此操作已创建,而不是在onCreateView中

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_profile, null);
    return view;;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
      nameAge =getActivity().findViewById(R.id.nameAge);
    occupation = getActivity().findViewById(R.id.occupation);
    description =getActivity().findViewById(R.id.description);


}

欢迎您。@BenjaminJenne,如果它是正确的,那么它就是答案!!!