我正在尝试用BottomNaviationView显示两个片段,一个是HomeFragment,另一个是AboutFragment。第一个是Ho和第二个mefragment,它也有tablayout,包含两个正常工作的片段。但现在我只需要底部导航视图中的两个主要片段:第一个HomeFragment和第二个Aboutfragment。
主页片段.java
public class HomeFragment extends Fragment {
FloatingActionButton add;
TabLayout tabLayout;
ViewPager2 viewPager2;
MyAdapter myAdapter;
public HomeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
add = getActivity().findViewById(R.id.addButton);
tabLayout = getActivity().findViewById(R.id.tabLayout);
viewPager2 = getActivity().findViewById(R.id.viewPager);
myAdapter = new MyAdapter(this);
viewPager2.setAdapter(myAdapter);//this is where error occured
//Attempt to invoke virtual method 'void androidx.viewpager2.widget.ViewPager2.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
add.setOnClickListener(view -> {
Intent intent = new Intent(getActivity().getApplicationContext(), AddMesurement.class);
startActivity(intent);
});
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager2.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
tabLayout.getTabAt(position).select();
}
});
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/homeFragment"
tools:context=".HomeFragment">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:elevation="3dp"
app:borderWidth="0dp"
android:src="@drawable/ic_baseline_note_add_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:backgroundTint="@color/purple_200"
android:contentDescription="@string/app_name" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/recent"
android:text="RECENT"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/confirmed"
android:text="CONFIRMED"/>
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager"
app:layout_constraintTop_toBottomOf="@+id/tabLayout"/>
</androidx.constraintlayout.widget.ConstraintLayout>
//这是我的主java文件,它有底栏,我正在从这个文件访问fregments主页.java
public class HomePage extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
bottomNavigationView = findViewById(R.id.bottomBar);
bottomNavigationView.setSelectedItemId(R.id.home);
fragmentReplace(new HomeFragment());
bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case (R.id.home):
fragmentReplace(new HomeFragment());
break;
case (R.id.about):
fragmentReplace(new AboutFragment());
break;
default:
fragmentReplace(new HomeFragment());
break;
}
return true;
}
});
}
private void fragmentReplace(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.commit();
}
}
1条答案
按热度按时间a64a0gku1#
您需要在onCreateView()中放大视图,然后从那里引用视图。
在这种情况下,getActivity().findid(…)不会切割奶酪。
祝你好运