Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Icon in tabLayout throws nullpointer exception

I'm creating a simple project like instagram, it has 3 tabs in main activity. I want to add icons with text in tablayout tabs, but it throws exception.

My MainActivity

public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


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

    TabLayout.Tab tab = tabLayout.getTabAt(0);

    tab.setIcon(new IconicsDrawable(this).icon(FontAwesome.Icon.faw_home));


    viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager(), getApplicationContext()));


}

private class CustomAdapter extends FragmentPagerAdapter {
    private String items[] = {"Home", "Upload", "Profile"};

    public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
        super(supportFragmentManager);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new fragment1();
            case 1:
                return new fragment2();
            case 2:
                return new fragment3();
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return items.length;
    }


    @Override
    public CharSequence getPageTitle(int position) {
        return items[position];
    }
}
  }

This Code Throws nullpointer exception what should i do ?

Exception Text

09-29 14:33:53.004 29528-29528/com.example.eyepatch.shutter E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.eyepatch.shutter, PID: 29528
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eyepatch.shutter/com.example.eyepatch.shutter.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.design.widget.TabLayout$Tab android.support.design.widget.TabLayout$Tab.setIcon(android.graphics.drawable.Drawable)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5530)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:733) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.design.widget.TabLayout$Tab android.support.design.widget.TabLayout$Tab.setIcon(android.graphics.drawable.Drawable)' on a null object reference
at com.example.eyepatch.shutter.MainActivity.onCreate(MainActivity.java:33) at android.app.Activity.performCreate(Activity.java:6272)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)  at android.app.ActivityThread.access$900(ActivityThread.java:157) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)  at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5530) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:733) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:623)

like image 942
Melfun Avatar asked Dec 21 '25 03:12

Melfun


1 Answers

You are supposed to first set the adapter to viewpager

Re-arrange the following lines:

tabLayout.setupWithViewPager(viewPager);
TabLayout.Tab tab = tabLayout.getTabAt(0);
tab.setIcon(new IconicsDrawable(this).icon(FontAwesome.Icon.faw_home));
viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager(), getApplicationContext()));

as given below:

viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager(), getApplicationContext()));
tabLayout.setupWithViewPager(viewPager);
TabLayout.Tab tab = tabLayout.getTabAt(0);
tab.setIcon(new IconicsDrawable(this).icon(FontAwesome.Icon.faw_home));
like image 116
iMDroid Avatar answered Dec 22 '25 17:12

iMDroid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!