(Viewpager Adapter) getSupportFragmentManager() returns NULL

1.3k views Asked by At

I want to make sliding tabs and here Im stuck :

Main.java

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.Window;


public class Main extends ActionBarActivity  implements TabListener, android.support.v7.app.ActionBar.TabListener {

    android.support.v7.app.ActionBar actionbar;
    ViewPager viewpager;

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



        viewpager = (ViewPager)findViewById(R.id.pager);
        viewpager.setAdapter(new FragmentAdapter(getSupportFragmentManager()));
        actionbar = getSupportActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        android.support.v7.app.ActionBar.Tab tab_1 = actionbar.newTab();
        android.support.v7.app.ActionBar.Tab tab_2 = actionbar.newTab();

        tab_1.setText("Tab 1");
        tab_2.setText("Tab 2");

        tab_1.setTabListener(this);
        tab_2.setTabListener(this);

        actionbar.addTab(tab_1);
        actionbar.addTab(tab_2);
    }


    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onTabReselected(android.support.v7.app.ActionBar.Tab arg0,
            android.support.v4.app.FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onTabSelected(android.support.v7.app.ActionBar.Tab arg0,
            android.support.v4.app.FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onTabUnselected(android.support.v7.app.ActionBar.Tab arg0,
            android.support.v4.app.FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }


}



class FragmentAdapter extends FragmentPagerAdapter{

    public FragmentAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        Fragment frag=null;

        if(arg0 == 0)
        {
            frag = new TAB_1();
        }

        if(arg0 == 1)
        {
            frag = new TAB_2();
        }

        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 2;
    }



}

activity_main.xml

<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/pager"
   android:layout_height = "match_parent"
   android:layout_width="match_parent" 

></android.support.v4.view.ViewPager>

I debuged my code and saw that the problem is in this line

viewpager.setAdapter(new FragmentAdapter(getSupportFragmentManager()));

The function getSupportFragmentManager() returns null . I made the class FragmentAdapter which extends from FragmentPagerAdapter.

My stacktrace :

06-09 14:15:16.536: E/AndroidRuntime(20977): FATAL EXCEPTION: main
06-09 14:15:16.536: E/AndroidRuntime(20977): Process: com.MX.KIK, PID: 20977
06-09 14:15:16.536: E/AndroidRuntime(20977): java.lang.NullPointerException: Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:416)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:411)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.support.v4.view.ViewPager.populate(ViewPager.java:987)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.View.measure(View.java:18417)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5801)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.View.measure(View.java:18417)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5801)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.View.measure(View.java:18417)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5801)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.View.measure(View.java:18417)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5801)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1835)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:725)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:616)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.View.measure(View.java:18417)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5801)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2881)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.View.measure(View.java:18417)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2237)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1295)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1537)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1180)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6558)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.Choreographer.doCallbacks(Choreographer.java:590)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.Choreographer.doFrame(Choreographer.java:560)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.os.Handler.handleCallback(Handler.java:739)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.os.Looper.loop(Looper.java:145)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at android.app.ActivityThread.main(ActivityThread.java:5832)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at java.lang.reflect.Method.invoke(Native Method)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at java.lang.reflect.Method.invoke(Method.java:372)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
06-09 14:15:16.536: E/AndroidRuntime(20977):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Thanks in advance !

2

There are 2 answers

0
Blackbelt On BEST ANSWER

you getItem has to return an instance of a Fragment, not null. Change it to:

@Override
public Fragment getItem(int arg0) {
    // TODO Auto-generated method stub
    Fragment frag=null;
    if (arg0 == 0) {
        frag = new TAB_1();
    } else if(arg0 == 1) {
        frag = new TAB_2();
    }
    return frag;
}
0
Pravesh On

The @Blackbelt is absolutely right...as you are returning null from the getItem method...you need to return the reference of the fragment from it.

@Override
public Fragment getItem(int arg0) {
    // TODO Auto-generated method stub
    Fragment frag=null;
    if (arg0 == 0) {
        frag = new TAB_1();
    } else if(arg0 == 1) {
        frag = new TAB_2();
    }
    return frag;
}

[http://developer.android.com/training/implementing-navigation/lateral.html]

follow this link to get more information regarding what you trying to achieve..