应用程序崩溃时将图标设置为标签布局

时间:2016-02-22 13:12:21

标签: android nullpointerexception android-tablayout

我正在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 $标签   在null上的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)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                    在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)                                                                                 引起:java.lang.NullPointerException:尝试调用虚拟   方法'android.support.design.widget.TabLayout $ Tab   在null上的android.support.design.widget.TabLayout.getTabAt(int)'   对象参考                                                                                    在   com.techsponential.cricbash.navHomeActivity.setupTabIcons(navHomeActivity.java:57)                                                                                    在   com.techsponential.cricbash.navHomeActivity.onCreate(navHomeActivity.java:40)                                                                                    在android.app.Activity.performCreate(Activity.java:6245)                                                                                    在   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)                                                                                    在android.os.Looper.loop(Looper.java:148)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:5443)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                               在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728)

     

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

1 个答案:

答案 0 :(得分:4)

在致电setupTabIcons()以检索findViewById()或添加任何标签之前,您正在呼叫TabLayout。尝试更改:

setupTabIcons();

tabLayout = (TabLayout) findViewById(R.id.tabHomeLayout);
tabLayout.setupWithViewPager(viewPager);

为:

tabLayout = (TabLayout) findViewById(R.id.tabHomeLayout);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();