Bryan
Bryan

Reputation: 3541

Start fragment from MainActivity

I have one fragment class and one activity class.

Here Is my fragment class:

public class LoginFragment extends Fragment {

    private TextView mTextDetails;

    private CallbackManager mCallbackManager;

    private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile(); //Access the profile who Is the person login in

            if(profile != null) {
                mTextDetails.setText("Welcome" + profile.getName());
            }
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException e) {

        }

    };

    public LoginFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
        mCallbackManager = CallbackManager.Factory.create();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.login, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);

        //Ask the use for permission to access friends
        loginButton.setReadPermissions("user_friends");

        //Because we work with fragments
        loginButton.setFragment(this);

        loginButton.registerCallback(mCallbackManager, mCallback);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mCallbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

I want to start this fragment from my MainActivity:

public class MainActivity extends FragmentActivity {

    CallbackManager callbackManager;

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

        FacebookSdk.sdkInitialize(getApplicationContext());

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

        fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();


    }
}

When I try to run my app, I get this error message:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.waddapp.bryan.waddapp/com.waddapp.bryan.waddapp.MainActivity}: java.lang.NullPointerException: Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        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)
 Caused by: java.lang.NullPointerException: Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference

What am I doing wrong? The fragmentContainer Is the ID on my RelatieLayoute located In my activity_main.xml

Upvotes: 0

Views: 2287

Answers (3)

rahul.ramanujam
rahul.ramanujam

Reputation: 5618

Try to use the support library fragments for backward compatibility. Extend support fragment in LoginFragment

    import android.support.v4.app.Fragment;

    public class LoginFragment extends android.support.v4.app.Fragment {

And use the supportFragmentManager for fragment transaction

 FragmentManager fm = getSupportFragmentManager();
 Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();

Upvotes: 0

Priya B
Priya B

Reputation: 96

The below code returns a null fragment as there is no fragment added to the view.

Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

Try the following:

LoginFragment fragment = new LoginFragment();
fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();

Upvotes: 1

Bryan
Bryan

Reputation: 3541

I found the problem. I forgot to Instantiate my fragment class.

I added this:

   if(fragment == null) {
            fragment = new LoginFragment();
            fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
        }

Upvotes: 0

Related Questions