这个问题可能很明显,但我仍有问题要找。我已经在两个不同的部分创建了主屏幕,“工具栏”和"fragmentLayout“。问题是,每当我想执行以下命令时,我都会得到一个错误"java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int PageAdapter“:"pageAdapter = new (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) {
}
这是.xml代码的mainActivity:
<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"/>
这是.xml of "HomeFragment":
<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>
我很感激每一个帮助。
发布于 2021-03-20 19:48:49
问题是,每当我想执行这个命令“com.google.android.material.tabs.TabLayout.getTabCount()‘= new (getSupportFragmentManager(),tabLayout.getTabCount())”时,都会得到一个错误"java.lang.NullPointerException:试图在空对象引用上调用虚拟方法'int PageAdapter“;
这意味着tabLayout
为null。即使调用在同一条线路上,也与getSupportFragmentManager()
无关。
您的tabLayout
位于HomeFragment
中,因此应该在该片段中设置选项卡布局,而不是在活动中设置选项卡布局。
https://stackoverflow.com/questions/66725627
复制相似问题