Java 在片段中获取TableLayout视图将返回NullPointerException
我试图将Java 在片段中获取TableLayout视图将返回NullPointerException,java,android,android-fragments,tabs,fragment,Java,Android,Android Fragments,Tabs,Fragment,我试图将TabLayout添加到片段中,我通过多个教程讨论了实现TabLayout的主题,它们大致相同。我按照教程进行操作,并更改了一些方法以适应片段而不是通常的活动,但编译后,它返回一个错误,即通过findViewById从xml文件获取Tablayout。尽管我检查了很多,但我还没有找到哪里做错了。TabLayout位于片段中,因为我使用BottomNavigation作为应用程序的主导航,但我希望使用TabLayout在片段中显示额外信息 这是保存表格布局的片段 public class
TabLayout
添加到片段中,我通过多个教程讨论了实现TabLayout
的主题,它们大致相同。我按照教程进行操作,并更改了一些方法以适应片段而不是通常的活动,但编译后,它返回一个错误,即通过findViewById
从xml文件获取Tablayout
。尽管我检查了很多,但我还没有找到哪里做错了。TabLayout
位于片段中,因为我使用BottomNavigation
作为应用程序的主导航,但我希望使用TabLayout
在片段中显示额外信息
这是保存表格布局的片段
public class LocationsFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//The next line has the NullPointerException.
TabLayout tabLayout = getView().findViewById(R.id.tabLayout);
TabItem discoveredTab = getView().findViewById(R.id.discoveredTab);
TabItem allLocationsTab = getView().findViewById(R.id.allTab);
ViewPager viewPager = getView().findViewById(R.id.viewPager);
PagerAdapter pagerAdapter = new PagerAdapter(getChildFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if (tab.getPosition() == 0){
pagerAdapter.notifyDataSetChanged();
} else if (tab.getPosition() == 1){
pagerAdapter.notifyDataSetChanged();
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
View root = inflater.inflate(R.layout.fragment_locations, container, false);
return root;
}
这是PagerAdapter课程
public class PagerAdapter extends FragmentPagerAdapter {
private int numOfTabs;
public PagerAdapter(FragmentManager fragmentManager, int numOfTabs){
super(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
this.numOfTabs = numOfTabs;
}
@NonNull
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new TabDiscoveredFragment();
case 1:
return new TabAllFragment();
default:
return null;
}
}
@Override
public int getCount() {
return numOfTabs;
}
}
这是布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".ui.journey.JourneyFragment">
<RelativeLayout
android:id="@+id/statLayout"
android:layout_width="match_parent"
android:layout_height="65sp"
android:layout_marginStart="20sp"
android:layout_marginEnd="20sp"
android:layout_marginTop="30sp"
android:background="@drawable/layout_bg_round_all">
//TextViews which are irrelevant to the example
</RelativeLayout>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/statLayout"
android:layout_marginTop="20sp">
<com.google.android.material.tabs.TabItem
android:id="@+id/discoveredTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Discovered" />
<com.google.android.material.tabs.TabItem
android:id="@+id/allTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="All locations" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/viewPager">
</androidx.viewpager.widget.ViewPager>
</RelativeLayout>
//与示例无关的文本视图
您不能在onCreateView()
片段生命周期方法中使用getView()
。。因为片段视图尚未创建;实际上,onCreateView()
创建此视图并将其返回
只能在onCreateView()之后的片段生命周期方法中使用getView()
要解决此问题,您需要使用膨胀视图本身,要将其应用于代码,请将getView()
替换为root
。。。但是在方法的一开始就声明它
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_locations, container, false);
//The next line has the NullPointerException.
TabLayout tabLayout = root.findViewById(R.id.tabLayout);
TabItem discoveredTab = root.findViewById(R.id.discoveredTab);
TabItem allLocationsTab = root.findViewById(R.id.allTab);
ViewPager viewPager = root.findViewById(R.id.viewPager);
PagerAdapter pagerAdapter = new PagerAdapter(getChildFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if (tab.getPosition() == 0){
pagerAdapter.notifyDataSetChanged();
} else if (tab.getPosition() == 1){
pagerAdapter.notifyDataSetChanged();
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
return root;
}
如果可以的话,我想再问一个问题。现在,在您的帮助下,选项卡可以工作了,但是选项卡中的内容被定位到父级的开头,但是它必须包含在一个相对的布局中的选项卡布局下方。@EdwardAarma对不起,我找不到您。。对于不清楚的解释,你能出示一些照片吗?现在我在问题的末尾加了一个截图。在那里你可以看到表格布局在屏幕的中间,标签应该从下面开始,但是你可以看到在左上角是标签的内容(“发现”一词)。你需要让VIEWPGER在Tab版图的下面添加“代码> Android:LayOutuBuff= = @ ID/TabPays:<代码>到VIEWPER。