我想在片段中创建一个选项卡式菜单。我试图将片段作为子项包含在mainactivity中,但是它没有用,然后我决定使用JobsTab类包含pageView及其适配器,并以java.lang结尾。 NullPointerException:尝试在空对象引用上调用虚拟方法“ android.view.View android.view.View.findViewById(int)”。谁能帮我解决这个问题
package com.alvartcollection.leakjob.Fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.alvartcollection.leakjob.PageAdapters.PageAdapter;
import com.alvartcollection.leakjob.R;
import com.google.android.material.tabs.TabLayout;
public class JobsTab extends Fragment {
private PageAdapter adapter;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View tabView = inflater.inflate(R.layout.jobstab,container,false);
viewPager = container.findViewById(R.id.viewPager);
adapter = new PageAdapter(getFragmentManager());
adapter.addFragment(new JobsTab(), "Tab 1");
adapter.addFragment(new MatchedTab(), "Tab 2");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
return tabView;
}
}