Program-Me-Rev
Program-Me-Rev

Reputation: 6624

How to add a Fragment in Android

I'm trying to add a fragment that contains a TabLayout View, but I keep getting the error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.design.widget.TabLayout$Tab android.support.design.widget.TabLayout.newTab()' on a null object reference
                                                                at ca.rev.revcore.MainActivity.onCreate(MainActivity.java:76)

This is the layout I'm trying to add:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rev_tablayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/RevStyle_PlaceNewBagBttn">

    <android.support.design.widget.TabItem
        android:id="@+id/tabItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tab1" />

</android.support.design.widget.TabLayout>

And this is how I'm trying to add it:

 TabLayout tabLayout = (TabLayout) findViewById(R.id.rev_tablayout);

 tabLayout.addTab(tabLayout.newTab().setText("Upload"));
 tabLayout.addTab(tabLayout.newTab().setText("Pics"));
 tabLayout.addTab(tabLayout.newTab().setText("Vids"));
 tabLayout.addTab(tabLayout.newTab().setText("Papers"));

 tabLayout.getTabAt(0).setIcon(R.drawable.ic_publish_black_24dp);
 tabLayout.getTabAt(1).setIcon(R.drawable.ic_menu_camera);
 tabLayout.getTabAt(2).setIcon(R.drawable.ic_videocam_black_24dp);
 tabLayout.getTabAt(3).setIcon(R.drawable.ic_airplay_black_24dp);

What could I be missing?

Vielen dank im voraus.

Upvotes: 0

Views: 115

Answers (4)

AskNilesh
AskNilesh

Reputation: 69689

If you are using fragment then. binding of controls like this in your oncreate view where you inflate your layout

View rootView = inflater.inflate(R.layout.fragment_, container, false);
TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tablayout);

Upvotes: 1

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Override onCreateView() method in Fragment, and inflate the Fragment layouts using LayoutInflater Class object and by pass­ing the ViewGroup argu­ment which is the activity in which the frag­ment will be embedded.

TabLayout tabLayout = (TabLayout) your_View_OBJ.findViewById(R.id.rev_tablayout);

Demo

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.your_xml_name, container, false);
    TabLayout tabLayout = (TabLayout)rootView.findViewById(R.id.rev_tablayout);
    return rootView;
}

Upvotes: 1

N.Moudgil
N.Moudgil

Reputation: 869

Is Tab Layout with id rev_tablayout in activity_main xml or is it in fragment's layout? please check where you are trying to access the tablayout.

Upvotes: 0

Piyush
Piyush

Reputation: 18933

If you are using fragment then. Change

TabLayout tabLayout = (TabLayout) findViewById(R.id.rev_tablayout);

to

TabLayout tabLayout = (TabLayout) view.findViewById(R.id.rev_tablayout);

view is the object which will inflate your layout file and return view in onCreateView() method.

Upvotes: 3

Related Questions