Red Virus Red Virus - 28 days ago 5
Android Question

Android - Tab items are not being created

I' am following a tutorial learning how to do a tab layout. I don't know what I did in the following code that it doesn't output the tabs. When the app runs, I don't see the tab item. Everything else works.

Acitvity

public class dashboard extends AppCompatActivity {

TabLayout tabs;
ViewPager container;

private SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;

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

// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(container);
tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab) {
container.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
container.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
container.setCurrentItem(tab.getPosition());
}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_dashboard, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";

public PlaceholderFragment() {
}

/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private String fragments [] = {"Completed", "In Progress"};
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
switch(position){
case 0:
return new dashboard_completed();
case 1:
return new dashboard_in_progress();
default:
return null;
}
}

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

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


dashboard_in_progress

public class dashboard_in_progress extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dashboard_in_progress, container, false);
}
}


dashboard_completed

public class dashboard_completed extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dashboard_completed, container, false);
}
}


XML



<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabs">

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

<view
android:layout_height="match_parent"
class="android.support.v4.view.ViewPager"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_marginTop="48dp"/>
</android.support.design.widget.CoordinatorLayout>

Answer

This is your problem.

ViewPager container;
...

private ViewPager mViewPager;
...

tabs.setupWithViewPager(container);

You're declaring two ViewPagers, but you're only initializing one, and trying to setup the TabLayout with the null one.

Remove the ViewPager container; declaration, and change the setup call to:

tabs.setupWithViewPager(mViewPager);

Also, you don't need to set the OnTabSelectedListener on the TabLayout yourself. The setupWithViewPager() method will handle that for you.