這個問題在這里已經有了答案: “java.lang.NullPointerException:嘗試在空物件參考上呼叫虛擬方法'...'”是什么意思,我該如何解決?[重復] (1 個回答) 2天前關閉。
我正在嘗試使用 BottomNaviagtionView 顯示兩個片段,一個是 HomeFragment,第二個是 AboutFragment。第一個是 Ho 和第二個 mefragment,它也有 tablayout 并包含兩個正常作業的片段。但現在我只想要來自 bottomNavigationview 1st HomeFragment 和 2nd Aboutfragment 的 2 個主要片段。
HomeFragment.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);
}
}
片段主頁.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檔案,它有底欄,我正在從這個檔案訪問片段。 主頁.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();
}
}
uj5u.com熱心網友回復:
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_home, container, false)
add = v.findViewById(R.id.addButton);
tabLayout = v.findViewById(R.id.tabLayout);
viewPager2 = v.findViewById(R.id.viewPager);
myAdapter = new MyAdapter(this);
viewPager2.setAdapter(myAdapter);
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 v; }
只需添加一些語法糖,因為這個編輯器沒有很好地粘貼代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/522121.html
標籤:爪哇安卓安卓布局android-recyclerview
上一篇:建構式在Fragment中未定義
