I'm starting in android development and want to put tabs with swipeable views, I followed a AndroidHive because of Google tutorial did not make the slightest meaning to me. The issue is that after doing the swipe my application is crashing, I tried to understand the logcat but did not find the error. Follow the code and logcat.
Logcat
07-11 13:46:24.218: E/AndroidRuntime(10550): FATAL EXCEPTION: main
07-11 13:46:24.218: E/AndroidRuntime(10550): Process: com.myeventsgw2, PID: 10550
07-11 13:46:24.218: E/AndroidRuntime(10550): java.lang.NullPointerException: Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference
07-11 13:46:24.218: E/AndroidRuntime(10550): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
07-11 13:46:24.218: E/AndroidRuntime(10550): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
07-11 13:46:24.218: E/AndroidRuntime(10550): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
07-11 13:46:24.218: E/AndroidRuntime(10550): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
07-11 13:46:24.218: E/AndroidRuntime(10550): at android.support.v4.view.ViewPager.populate(ViewPager.java:1053)
07-11 13:46:24.218: E/AndroidRuntime(10550): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:555)
07-11 13:46:24.218: E/AndroidRuntime(10550): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
07-11 13:46:24.218: E/AndroidRuntime(10550): at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
07-11 13:46:24.218: E/AndroidRuntime(10550): at com.myeventsgw2.MainActivity.onTabSelected(MainActivity.java:72)
07-11 13:46:24.218: E/AndroidRuntime(10550): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
07-11 13:46:24.218: E/AndroidRuntime(10550): at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1105)
07-11 13:46:24.218: E/AndroidRuntime(10550): at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:547)
07-11 13:46:24.218: E/AndroidRuntime(10550): at android.view.View.performClick(View.java:4445)
07-11 13:46:24.218: E/AndroidRuntime(10550): at android.view.View$PerformClick.run(View.java:18429)
07-11 13:46:24.218: E/AndroidRuntime(10550): at android.os.Handler.handleCallback(Handler.java:733)
07-11 13:46:24.218: E/AndroidRuntime(10550): at android.os.Handler.dispatchMessage(Handler.java:95)
07-11 13:46:24.218: E/AndroidRuntime(10550): at android.os.Looper.loop(Looper.java:136)
07-11 13:46:24.218: E/AndroidRuntime(10550): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-11 13:46:24.218: E/AndroidRuntime(10550): at java.lang.reflect.Method.invoke(Native Method)
07-11 13:46:24.218: E/AndroidRuntime(10550): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-11 13:46:24.218: E/AndroidRuntime(10550): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
MainActivity
package com.myeventsgw2;
import com.myeventsgw2.adapter.TabsPagerAdapter;
import com.myeventsgw2.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
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(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
TabsPagerAdapter
package com.myeventsgw2.adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.myeventsgw2.GamesFragment;
import com.myeventsgw2.MoviesFragment;
import com.myeventsgw2.TopRatedFragment;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top rated
return new TopRatedFragment();
case 1:
// second
return new GamesFragment();
case 3:
// third
return new MoviesFragment();
}
return null;
}
@Override
public int getCount() {
//number of tabs
return 3;
}
}
TopRatedFragment (tab example)
package com.myeventsgw2;
import com.myeventsgw2.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TopRatedFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated,
container, false);
return rootView;
}
}