Reputation: 172
I'm trying to define an onCreateView for a fragment.
the code:
package com.example.weekplanner;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class FragmentChart extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=new inflater.inflate(R.layout.home_fragment_layout,container,false); //error on this line
return view;
}
}
but the inflate
method cannot be resolved.
home_fragment_layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 0
Views: 43
Reputation: 451
Remove new from inflater object
change this line
View view=new inflater.inflate(R.layout.home_fragment_layout,container,false);
to this
View view=inflater.inflate(R.layout.home_fragment_layout,container,false);
Upvotes: 2
Reputation: 768
inflator
is the object of LayoutInflator
class that is already passed in the onCreateView
method. Therefore, no need to use new
keyword before it, because it's a object (not a class).
Thus, your code must be like this,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.home_fragment_layout,container,false);
return view;
}
Upvotes: 1