gpt4 book ai didi

java - Android fragment oncreateview 被调用,但 View 未膨胀并且没有错误

转载 作者:行者123 更新时间:2023-12-02 11:37:00 25 4
gpt4 key购买 nike

我已经这样做过很多次了,但这次我真的被难住了。从我的 Activity 中调用我的 fragment 没有任何问题,尽管调用了 onCreateView() ,但什么也没有显示,仍然没有错误。我花了几个小时在这里寻找类似的问题,但没有成功。

这是调用我的 fragment 的 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>

这是我的 onClickListener 调用 swapFragment():

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

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

最佳答案

我终于找到了答案,我所要做的就是将我的根元素(线性布局)更改为框架布局,它就工作了。希望这对某人有帮助。感谢大家的建议。

关于java - Android fragment oncreateview 被调用,但 View 未膨胀并且没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48867340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com