I am adding a tab view or (ViewPager) to my Fragment in an App I am working on.
I want to put the tab view inside the Contact Fragment
I have a Navigation Drawer that opens, and you see list of options...
Home
Services
Contact
Everything you click goes to a fragment
Then in that fragment loads the .XML file, with a corresponding class to program everything inside the .XML file.
I was following an online tutorial [CLICK HERE][3]
Obviously it was over 2 years ago. Android Code has changed since then. I started with a few more errors then what I have now.
I am experiencing an issue with getSupportFragmentManager(), and getActionBar() really confused at this point, all imports are there. It won't recognize those two. Obviously that makes me think well something is missing, but I am not sure what needs to put to allow the compiler to see those needed methods.
I still get Null when the ViewPager setAdapter is set.
All I am trying to accomplish is a tab view, with two tabs for Contact, and Social Media. If I could get a simplistic understanding of what is required for tabs to work properly.
I have already searched questions and they don't exactly touch on my specific issue, and so I am asking for help. Any help is definitely appreciated.
FragmentContact
package com.megliosolutions.bitsnbytes.Fragments;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.megliosolutions.bitsnbytes.MainActivity;
import com.megliosolutions.bitsnbytes.R;
import com.megliosolutions.bitsnbytes.ViewPagerClasses.TabsPagerAdapter;
/**
* Created by Jarvis on 6/21/15.
*/
public class FragmentContact extends Fragment {
private ViewPager viewPager;
TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = {"Contact", "Social Media"};
public static FragmentContact newInstance(){
FragmentContact fragment = new FragmentContact();
return fragment;
}
public FragmentContact(){};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_contact, container, false);
LoadTabs(rootView);
return rootView;
}
private void LoadTabs(View rootView) {
FragmentManager fragmentManager = getSupportFragmentManager();
// Initilization
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(fragmentManager);
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener((ActionBar.TabListener) this));
}
}
@Override
public void onAttach(FragmentActivity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(5);
}
}
TabsPagerAdapter
package com.megliosolutions.bitsnbytes.ViewPagerClasses;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
/**
* Created by Jarvis on 6/28/15.
*/
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Contact fragment activity
return new ContactFragment();
case 1:
// Social Media fragment activity
return new SocialMediaFragment();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
}
MAINACTIVITY
package com.megliosolutions.bitsnbytes;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.megliosolutions.bitsnbytes.Fragments.FragmentAbout;
import com.megliosolutions.bitsnbytes.Fragments.FragmentContact;
import com.megliosolutions.bitsnbytes.Fragments.FragmentHome;
import com.megliosolutions.bitsnbytes.Fragments.FragmentProducts;
import com.megliosolutions.bitsnbytes.Fragments.FragmentServices;
import com.megliosolutions.bitsnbytes.Fragments.FragmentWarranties;
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
private ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
if(position == 0) {
fragmentManager.beginTransaction()
.replace(R.id.container, FragmentHome.newInstance())
.commit();
}
else if(position == 1){
fragmentManager.beginTransaction()
.replace(R.id.container, FragmentServices.newInstance())
.commit();
}
else if(position == 2){
fragmentManager.beginTransaction()
.replace(R.id.container, FragmentProducts.newInstance())
.commit();
}
else if(position == 3){
fragmentManager.beginTransaction()
.replace(R.id.container, FragmentWarranties.newInstance())
.commit();
}
else if(position == 4){
fragmentManager.beginTransaction()
.replace(R.id.container, FragmentContact.newInstance())
.commit();
}
else if(position == 5){
fragmentManager.beginTransaction()
.replace(R.id.container, FragmentAbout.newInstance())
.commit();
}
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
case 4:
mTitle = getString(R.string.title_section4);
break;
case 5:
mTitle = getString(R.string.title_section5);
break;
case 6:
mTitle = getString(R.string.title_section6);
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@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";
/**
* 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;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
Now there is an issue with the FragmentContact.newInstance() in onNavigationDrawerItemSelected()
06-28 15:35:24.695 11744-11744/com.megliosolutions.bitsnbytes E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.megliosolutions.bitsnbytes, PID: 11744
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at com.megliosolutions.bitsnbytes.Fragments.FragmentContact.LoadTabs(FragmentContact.java:55)
at com.megliosolutions.bitsnbytes.Fragments.FragmentContact.onCreateView(FragmentContact.java:43)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
from logcat Error its clear ViewPager not exist in R.layout.fragment_contact. Please verify that.
Since I need to refer to the viewPager in which it exists on activity_main.xml not the current fragment, I manage to solve this error:
I added a declaration for a new view referring to the activity where the ViewPager exists as following:
Then, I declared the viewPager with regards the above declaration:
After that, I set up the adaptor.