简体   繁体   中英

Android fragment oncreateview is called but view is not inflated and there are no errors

So I've done this a fair amount of times but I'm truly stumped this time. My fragment is called from my activity with no problems and although the onCreateView() is called, nothing at all shows up, still no errors. I've spent a couple hours looking for similar questions here with no luck.

Here is my swapFragment() method that calls my fragment:

private void swapFragment() {
        GenresFragment genresFragment = new GenresFragment();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.flgenre, genresFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

Here is my GenresFragment.java:

public class GenresFragment extends Fragment {

    // TODO: Customize parameter argument names
    private static final String ARG_COLUMN_COUNT = "column-count";
    // TODO: Customize parameters
    private int mColumnCount = 1;
    private OnListFragmentInteractionListener mListener;
    private DatabaseReference mPostReference;
    String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
    private ArrayList<String> genreList = new ArrayList<>();
    private MyGenreRecyclerViewAdapter myGenreRecyclerViewAdapter;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public GenresFragment() {
    }

    // TODO: Customize parameter initialization
    @SuppressWarnings("unused")
    public static GenresFragment newInstance(int columnCount) {
        GenresFragment fragment = new GenresFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_COLUMN_COUNT, columnCount);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_mini_add, container, false);

        if (rootView == null){
            Log.e("TEST1", "Is Null");
        }else {
            Log.e("TEST2", "Not Null");
        }

        return rootView;
    }

    public void failure(){
        Toast.makeText(getActivity(),"Something Went Wrong",Toast.LENGTH_LONG).show();
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnListFragmentInteractionListener) {
            mListener = (OnListFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnListFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnListFragmentInteractionListener {
        // TODO: Update argument type and name
        void onListFragmentInteraction(String item);
    }
}

And my fragment_mini_add.xml:

<FrameLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#80000000"
    tools:context="com.nocrat.fanti.ProfileActivity"
    android:id="@+id/flgenre">

    <!-- TODO: Update blank fragment layout -->


    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_gravity="center">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="14dp"
            android:text="@string/add_new_project"
            android:textAlignment="center"
            android:textColor="#000000"
            android:textSize="24sp" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:id="@+id/rLayout1"
            android:layout_below="@id/textView4">

            <EditText
                android:id="@+id/editText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:hint="@string/project_name"
                android:gravity="center_vertical"
                />

            <EditText
                android:id="@+id/editText2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:gravity="center_vertical"
                android:hint="@string/group_name"
                android:inputType="textPersonName"
                android:layout_below="@id/editText"/>


        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="bottom"
            android:padding="10dp"
            android:layout_below="@id/rLayout1">

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/create"
                android:textColor="#FFD700"
                android:textSize="18sp" />

            <Space
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/cancel"
                android:textColor="#FFD700"
                android:textSize="18sp" />
        </LinearLayout>
    </RelativeLayout>

</FrameLayout>

Here is my onClickListener that calls swapFragment():

addGenre.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                swapFragment();
            }
        });

Here is my activity xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="false" android:background="#ffffff" android:orientation="vertical" android:id="@+id/flgenre"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" android:elevation="40dp" android:background="#ffffff"> <include layout="@layout/app_bar_profile" android:layout_height="190dp" android:layout_width="match_parent" android:id="@+id/app1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" android:layout_margin="20dp" android:elevation="20dp" android:background="#ffffff" android:padding="10dp"> <LinearLayout android:layout_width="50dp" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/imageView3" android:layout_width="50dp" android:layout_height="50dp" android:layout_weight="1" app:srcCompat="@drawable/icons8musicalnotes64" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView8" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Genres" android:layout_gravity="center" android:gravity="center_vertical" android:padding="5dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@drawable/rounded_genres" android:gravity="center" android:clickable="true" android:id="@+id/addGenres"> <ImageView android:id="@+id/imageView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" app:srcCompat="@drawable/ic_add_white_48px" /> <TextView android:id="@+id/textView14" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Add" android:layout_gravity="center" android:gravity="center_vertical" android:padding="5dp" android:textColor="#ffffff"/> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" android:layout_margin="20dp" android:elevation="20dp" android:background="#ffffff" android:padding="10dp"> <LinearLayout android:layout_width="50dp" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/imageView5" android:layout_width="50dp" android:layout_height="50dp" android:layout_weight="1" app:srcCompat="@drawable/icons8resume64" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:focusableInTouchMode="true"> <TextView android:id="@+id/textView7" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center_vertical" android:padding="5dp" android:text="Bio" /> <EditText android:id="@+id/textView11" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:clickable="false" android:enabled="false" android:focusable="false" android:focusableInTouchMode="false" android:hint="Tell us about yourself..." android:textColor="#000000" android:textSize="14sp" /> <TextView android:id="@+id/textView12" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="edit" android:textColor="#FF00FF" android:layout_gravity="right" android:gravity="right" android:padding="5dp"/> <TextView android:id="@+id/textView13" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="save" android:textColor="#FF00FF" android:layout_gravity="right" android:gravity="right" android:padding="5dp" android:visibility="gone"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" android:layout_margin="20dp" android:elevation="20dp" android:background="#ffffff" android:padding="10dp"> <LinearLayout android:layout_width="50dp" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/imageView6" android:layout_width="50dp" android:layout_height="50dp" android:layout_weight="1" app:srcCompat="@drawable/icons8trophy64" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView9" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Average Rank" android:layout_gravity="center" android:gravity="center_vertical" android:padding="5dp"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center" android:layout_margin="20dp" android:elevation="20dp" android:background="#ffffff" android:padding="10dp"> <LinearLayout android:layout_width="50dp" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/imageView7" android:layout_width="50dp" android:layout_height="50dp" android:layout_weight="1" app:srcCompat="@drawable/icons8meeting64" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView10" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Previous Collaborations" android:layout_gravity="center" android:gravity="center_vertical" android:padding="5dp"/> </LinearLayout> </LinearLayout> </LinearLayout> </ScrollView> </LinearLayout> 

I finally figured out the answer, all I had to do was change my root element(linear layout) to frame layout and it worked. Hope this helps someone. Thanks to everyone for their suggestions.

Try to modify you swapFragment() with below code by replacing replace() with add()

private void swapFragment() {
    GenresFragment genresFragment = new GenresFragment();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.flgenre, genresFragment);
    fragmentTransaction.commit();
}

Other people already asked for the activity xml, but for what I could see right now, it looks like you are inflating the fragment's layout in a FrameLayout that is contained in it's own layout.

Meaning, the container you are inflating into should not be in the fragment's layout, but in the activity's layout.

Unless I understood it wrong, you should not have the with id/flgenre in fragment_mini_add.xml but in Activity_main.xml (or whatever the layout of the activity is)

Edit: in both activity and the fragment_mini_add.xml there is a layout with the same id you are trying to inflate into. I'm not sure if this causes the problem, but I imagine this could be the cause.

You should remove android:id="@+id/flgenre" in the first xml tag from your activity xml and fragment_mini_add.xml .

replace tools:context="com.nocrat.fanti.ProfileActivity" with tools:context="com.nocrat.fanti.GenresFragment" in fragment_mini_add.xml .

then, add this code to your activity xml to contain the fragment

<FrameLayout android:id="@+id/flgenre" android:layout_width="match_parent" android:layout_height="match_parent" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM