我在android中使用導航視圖創建了一個tabLayout。我想將圖標設置爲選項卡布局的選項卡。當我使用setIcon函數時,該應用程序拋出空指針異常並崩潰。請幫幫我。應用程序崩潰將圖標設置爲選項卡布局
public class navHomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private TabLayout tabLayout;
private ViewPager viewPager;
int icons[] = {
R.drawable.icon1,
R.drawable.icon2,
R.drawable.icon3};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.homeViewPager);
setupViewPager(viewPager);
setupTabIcons();
tabLayout = (TabLayout) findViewById(R.id.tabHomeLayout);
tabLayout.setupWithViewPager(viewPager);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(icons[0]);
tabLayout.getTabAt(1).setIcon(icons[1]);
tabLayout.getTabAt(2).setIcon(icons[2]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(), "tab1");
adapter.addFrag(new OneFragment(), "tab2");
adapter.addFrag(new OneFragment(), "tab3");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
Fragment fragment = null;
int id = item.getItemId();
if (id == R.id.nav_home)
{
setTitle("Home");
}
else if (id == R.id.frag2)
{
}
else if (id == R.id.frag3)
{
}
else if (id == R.id.frag4)
{
}
else if (id == R.id.frag5)
{
}
else if (id == R.id.frag6)
{
}
else if (id == R.id.frag7)
{
}
else if (id == R.id.frag8)
{
}
try {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.homeContentLayout, fragment).commit();
}catch (NullPointerException e){
Log.d("Exception", e.toString());}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
LogCat:
致命異常:主 了java.lang.RuntimeException:無法啓動活動 ComponentInfo {com.techsponential.cricbash/com.techsponential.cricbash.navHomeActivity}: 顯示java.lang.NullPointerException:嘗試調用虛擬方法 'android.support.design.widget.TabLayout $標籤 android.support.design.widget.TabLayout.getTabAt(INT)' 上的空 對象引用 在 android.app.ActivityThread.performLaunchActivity( ActivityThread.java:2426) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1354 ) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5443) 在java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteI nit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 引起:java.lang.NullPointerException:嘗試調用虛擬方法'android.support.design.widget的 .TabLayout $標籤 android.support.design.widget.TabLayout.getTabAt(INT)」上在 com.techsponential.cricbash.navHomeActivity.setupTabIcons(navHomeActivity.java:57) 在 COM空 對象引用 。 techsponential.cricbash.navHomeActivity.onCreate(navHomeActivity.java:40) at android.app.Activity.performCreate(Activity.java:6245) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
在android.app.ActivityThread.-wrap11(ActivityThread.java) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1354) 在android.os.Handler.dispatchMessage(Handler.java: 102) at android.os.Looper.loop(Looper.java:148) at android.app.A ctivityThread.main(ActivityThread.java:5443) 在java.lang.reflect.Method.invoke(本機方法) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit。Java的:618)
感謝的人!修復。 –