简体   繁体   English

设置标签图标时出现空指针异常

[英]Getting a null pointer exception when setting up the tab icons

I am trying to setup icons in the tabs, but I am keep getting null pointer exception.I am unable to understand as to why is happening? 我正在尝试在选项卡中设置图标,但是我一直在获取空指针异常。我无法理解为什么会发生?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d(TAG, "onCreate: starting");
    setupNavigationView();
    setupViewPager();

}

//Responsible for adding the 3 tabs:camera,main,message
private void setupViewPager(){
    SectionPagerAdapter sectionPagerAdapter = new SectionPagerAdapter(getSupportFragmentManager());
    sectionPagerAdapter.addFragment(new CameraFragment());
    sectionPagerAdapter.addFragment(new MainFragment());
    sectionPagerAdapter.addFragment(new MessagesFragment());
    ViewPager viewPager = findViewById(R.id.container);
    viewPager.setAdapter(sectionPagerAdapter);

    TabLayout layout = findViewById(R.id.tabs);
    layout.setupWithViewPager(viewPager);

    layout.getTabAt(0).setIcon(R.drawable.ic_camera);
    layout.getTabAt(1).setIcon(R.drawable.ic_instagram);
    layout.getTabAt(2).setIcon(R.drawable.ic_action_name);


}

Error: 错误:

  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
                                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.design.widget.TabLayout$Tab android.support.design.widget.TabLayout$Tab.setIcon(int)' on a null object reference
                                                                                   at android.vishal.instagramclone.MainActivity.MainActivity.setupViewPager(MainActivity.java:47)
                                                                                   at android.vishal.instagramclone.MainActivity.MainActivity.onCreate(MainActivity.java:31)

You are facing this issue because you have not added tabs and try to set icon 由于没有添加标签并尝试设置图标,因此您面临此问题

//Add tabs icon with setIcon() or simple text with .setText()
 tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.ic_home));
 tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.ic_profile));
 tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.ic_settings));

For further details check the link below 有关更多详细信息,请查看下面的链接

https://gist.github.com/faizsiddiqui/9af9cea9051335e1ad3f https://gist.github.com/faizsiddiqui/9af9cea9051335e1ad3f

To get the view either use: 要获取视图,请使用:

layout.getChildAt(0).getChildAt(0).setIcon(R.drawable.ic_camera);
layout.getChildAt(0).getChildAt(1).setIcon(R.drawable.ic_instagram);

or 要么

layout.getChildAt(0).findViewById(R.id.your_camera_tab_id).setIcon(R.drawable.ic_camera);
layout.getChildAt(0).findViewById(R.id.your_instagram_tab_id).setIcon(R.drawable.ic_instagram);

layout.getChildAt(0) returns the ViewGroup holding the Tabs. layout.getChildAt(0)返回包含选项卡的ViewGroup。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM