调用Android片段oncreateview但是视图没有膨胀且没有错误

时间:2018-02-19 13:31:37

标签: java android android-fragments layout-inflater

所以我已经做了相当多次,但这次我真的很难过。我的片段是从我的活动中调用的,没有任何问题,虽然onCreateView()被调用,但什么都没有显示,仍然没有错误。我花了几个小时在这里寻找类似的问题而没有运气。

以下是调用我的片段的swapFragment()方法:

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

这是我的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);
    }
}

我的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>

这是我调用swapFragment()的onClickListener:

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

这是我的活动xml:

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:1)

我终于想出了答案,我所要做的就是将我的根元素(线性布局)更改为框架布局并且它有效。希望这有助于某人。感谢大家的建议。

答案 1 :(得分:0)

尝试使用以下代码修改swapFragment(),将replace()替换为add()

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

答案 2 :(得分:0)

其他人已经要求xml这个活动,但是对于我现在看到的内容,看起来你在FrameLayout中包含了它自己的布局中的片段布局。

意思是,您要膨胀的容器不应位于片段的布局中,而应位于活动的布局中。

除非我理解错误,否则你不应该在fragment_mini_add.xml中使用with id / flgenre,而是在Activity_main.xml中(或者活动的布局是什么)

编辑:在活动和fragment_mini_add.xml中都有一个您尝试向其扩展的ID相同的布局。我不确定这是否会导致问题,但我想这可能是原因。

答案 3 :(得分:0)

您应该从android:id="@+id/flgenre"activity xml删除第一个xml标记中的fragment_mini_add.xml

tools:context="com.nocrat.fanti.ProfileActivity"替换为tools:context="com.nocrat.fanti.GenresFragment"中的fragment_mini_add.xml

然后,将此代码添加到您的活动xml以包含片段

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