javaandroidandroid-studioandroid-fragmentslayout-inflater

inflater.inflate(R.layout.) doesn't find Fragment.xml


I have the Problem that the command view =inflater.inflate(R.layout.doesn't find my Fragment xml

here is the Error:

enter image description here

public class FragmentName extends Fragment {

    View view;

    public FragmentName() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view =inflater.inflate(R.layout.name_fragment,container,false);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

}

This is my XML

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

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/view_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Charackter Name"
        android:textColor="@color/tabtextcolor"
        android:textSize="20sp"
        android:textStyle="bold"></com.google.android.material.textview.MaterialTextView>
    </LinearLayout>

I dont find my mistake. I rebuilded and cleaned the project.


Solution

  • Don't

    return super.onCreateView(inflater, container, savedInstanceState);
    

    Do

    retun View's object
    

    Rectify your onCreateView() method.

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
        view = inflater.inflate(R.layout.name_fragment, container, false);
        MaterialTextView view_id = (MaterialTextView) view.findViewById(R.id.view_id);
        return view;
    

    Then Clean-Rebuild Your IDE.