我正在尝试在我的主要活动中将片段加载到我的框架布局“ main_container”中。但是活动未在其中加载框架布局。如您所见,我尝试通过为框架布局提供黑色背景来显示活动中的框架布局。但是执行应用程序时,我无法查看框架布局。问题是当在任何设备上执行应用程序时,我无法查看在activity_main中添加的任何组件。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<FrameLayout
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_main);
MainFragment mainFragment = new MainFragment(MainActivity.this);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.main_container, mainFragment);
fragmentTransaction.addToBackStack(mainFragment.toString());
fragmentTransaction.commit();
}
}
MainFragment.java
public class MainFragment extends Fragment implements MainRecyclerViewAdapter.RecyclerViewClickListener {
private RecyclerView recyclerView;
private ArrayList<DataModel> arrayList;
private LinearLayout mainLayout;
private Context mContext;
public MainFragment(Context mContext) {
this.mContext = mContext;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
return v;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = (RecyclerView) view.findViewById(R.id.main_rv);
mainLayout = view.findViewById(R.id.ll_fragment_main);
arrayList = new ArrayList<>();
arrayList.add(new DataModel("Diseases", R.drawable.ic_disease));
arrayList.add(new DataModel("Diet Plan", R.drawable.ic_diet_plan));
arrayList.add(new DataModel("Nutritions", R.drawable.ic_nutrition));
arrayList.add(new DataModel("Food Info", R.drawable.ic_nutrient_sources));
MainRecyclerViewAdapter adapter = new MainRecyclerViewAdapter(getContext(), arrayList, this);
recyclerView.setAdapter(adapter);
AutoFitGridLayoutManager layoutManager = new AutoFitGridLayoutManager(getContext(), 500);
recyclerView.setLayoutManager(layoutManager);
}
@Override
public void onClick(View view, int position) {
switch (position) {
case 0:
Message.successMessage(mainLayout, "Disease");
break;
case 1:
Message.successMessage(mainLayout, "Diet Plan");
break;
case 2:
Message.successMessage(mainLayout, "Nutritions");
break;
case 3:
Message.successMessage(mainLayout, "Food Info");
break;
}
}
}
谢谢。
答案 0 :(得分:0)
您要覆盖活动的不正确的onCreate方法。
覆盖此-
@Override
protected void onCreate(Bundle savedInstanceState) {
}
代替
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable
PersistableBundle persistentState)