0

When I try to create an intent on my Fragment class to move my activity page HomeScreenActivity using,

Intent intent = new Intent(getActivity(), HomeScreenActivity.class);
startActivity(intent);
getActivity().finish(); 

I'm getting following error Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference on Fabric. I got this error on Galaxy S7 Edge with os version 7. While executing line Intent intent = new Intent(getActivity(), HomeScreenActivity.class); (Line number 338 on LoginFragment.java)

Here is the full log I got from on Fabric,

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
       at android.content.ComponentName.<init>(ComponentName.java:128)
       at android.content.Intent.<init>(Intent.java:5359)
       at com.example.android.fragments.LoginFragment.handleValidateEmployeeResponse(LoginFragment.java:338)
       at com.example.android.fragments.LoginFragment.access$100(LoginFragment.java:53)
       at com.example.android.fragments.LoginFragment$3.onResponse(LoginFragment.java:249)
       at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
       at android.os.Handler.handleCallback(Handler.java:751)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6692)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
7
  • Possible duplicate of android.content.Context.getPackageName()' on a null object reference Commented Nov 29, 2017 at 7:10
  • @JohnJoe : How to solve this ? How to avoid duplication? Commented Nov 29, 2017 at 7:20
  • Have you tried MyActivity.this ? Commented Nov 29, 2017 at 7:22
  • Please show the entire handleValidateEmployeeResponse method Commented Nov 29, 2017 at 7:34
  • @John This code is in a Fragment class Commented Nov 29, 2017 at 7:35

2 Answers 2

1

Try to use attach method

public Activity mActivity;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.mActivity = activity;
}

And use like this

Intent intent = new Intent(mActivity, HomeScreenActivity.class);
startActivity(intent);
mActivity.finish();
Sign up to request clarification or add additional context in comments.

2 Comments

Let me try this method.
Good luck to [email protected]
1

I did something like this,

Take Activity reference like

MainActivity activity = (MainActivity) getActivity();

Then pass the object where you want it, like

Intent intent = new Intent(activity , HomeScreenActivity.class);
activity.finish();

1 Comment

getActivity() will still return null depending where it's used

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.