java - 为什么 getSupportFragmentManager() 返回 NULL
问题描述
问题可能很明显,但我仍然有问题要找到它。我在两个不同的部分创建了主屏幕,“工具栏”和“fragmentLayout”。问题是,每当我想执行此操作时,我都会收到错误“java.lang.NullPointerException: Attempt to invoke virtual method 'int com.google.android.material.tabs.TabLayout.getTabCount()' on an null object reference”命令:“pageAdapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount());”
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private TabLayout tabLayout;
private ViewPager viewPager;
private TabItem tab1, tab2;
public PageAdapter pageAdapter;
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
navigationView.setCheckedItem(R.id.nav_home);
}
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tab1 = (TabItem) findViewById(R.id.tab1);
tab2 = (TabItem) findViewById(R.id.tab2);
viewPager = (ViewPager) findViewById(R.id.viewPager);
pageAdapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pageAdapter);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if(tab.getPosition() == 0)
{
pageAdapter.notifyDataSetChanged();
}
else if(tab.getPosition() == 1)
{
pageAdapter.notifyDataSetChanged();
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
这是 mainActivity 的 .xml 代码:
<androidx.drawerlayout.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:fitsSystemWindows="false"
tools:context=".MainActivity"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/green"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#5E5E5E"
android:id="@+id/nav_view"
app:headerLayout="@layout/main_nav_drawer"
app:menu="@menu/drawer_menu"
app:itemTextColor="@android:color/white"
app:itemTextAppearance="@style/NavText"/>
</androidx.drawerlayout.widget.DrawerLayout>
这是“HomeFragment”的 .xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:layout_constraintBottom_toTopOf="parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="@color/beige">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@drawable/tab_background"
app:tabIndicatorColor="@color/black"
app:tabSelectedTextColor="@color/black">
<com.google.android.material.tabs.TabItem
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/recipes_tab" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fav_tab" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.viewpager.widget.ViewPager>
我感谢每一个帮助。
解决方案
问题是,每当我想执行此操作时,我都会收到错误“java.lang.NullPointerException: Attempt to invoke virtual method 'int com.google.android.material.tabs.TabLayout.getTabCount()' on an null object reference”命令:“pageAdapter = new PageAdapter(getSupportFragmentManager(), tabLayout.getTabCount());”
这意味着它tabLayout
是空的。getSupportFragmentManager()
即使通话在同一条线上,也没有任何关系。
你tabLayout
在,HomeFragment
所以你应该在那个片段中设置你的标签布局,而不是在你的活动中。
推荐阅读
- ios - 如何使用最小最大几何边界创建 CLCircularRegion
- c# - 调用构造函数时使用字符串作为类型的参数
- javascript - React:静音/取消静音 youtube 嵌入播放器,无需重新渲染整个组件
- reactjs - 如何通过 cloneelement 获得对功能组件的引用?
- javascript - 从函数返回多个对象
- javascript - 将格式为 `YYYYMMDD` 的日期字符串转换为 `DD/MM/YYYY`
- java - 使 geoQuery.addGeoQueryEventListener() 成为阻塞操作
- ios - 如何在 React Native 中将远程 GIF 保存到 iOS 中的相册?
- c++ - 如何杀死/停止终端中正在运行的进程而不丢失其输出?
- node.js - 使用 Apache 在 CentOS 8 服务器中安装 Rocket.chat 时出现 MongoDB 错误