Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName() on getActivity()

SHIDHIN.T.S

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)
KeLiuyue

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();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

From Dev

NAVIGATION DRAWER :- NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()

From Dev

Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()

From Dev

Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)

From Dev

Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object

From Dev

Android exception Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)

From Dev

error:Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

From Java

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager

From Dev

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser

From Dev

java.lang.NullPointerException: Attempt to invoke virtual method android

From Dev

Android. Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

From Dev

Android. Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

From Dev

Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences

From Dev

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()'

From Dev

com.android.volley.VolleyError:java.lang.NullPointerException:Attempt to invoke virtual method 'in java.lang.String.length()' on object reference

From Dev

com.android.volley.VolleyError:java.lang.NullPointerException:Attempt to invoke virtual method 'in java.lang.String.length()' on object reference

From Dev

Service (Android Notification): Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

From Dev

Attempt to invoke virtual method 'java.lang.String java.lang.String.toUpperCase()' on a null object reference

From Dev

Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference

From Dev

'java.lang.String android.content.Context.getPackageName()' on a null object reference

From Dev

Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference

From Dev

Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference

From Dev

Error: Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference

From Dev

NullPointerException: Attempt to invoke virtual method 'java.lang.String com.zopim.android.sdk.model.VisitorInfo.getEmail()' app is forced close

From Dev

Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference on Volley while executing Swiperefresh Android

From Dev

Android: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

From Dev

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference

From Dev

Exception: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

From Dev

Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

Related Related

  1. 1

    Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

  2. 2

    NAVIGATION DRAWER :- NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()

  3. 3

    Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()

  4. 4

    Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)

  5. 5

    Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object

  6. 6

    Android exception Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)

  7. 7

    error:Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

  8. 8

    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager

  9. 9

    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser

  10. 10

    java.lang.NullPointerException: Attempt to invoke virtual method android

  11. 11

    Android. Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

  12. 12

    Android. Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

  13. 13

    Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences

  14. 14

    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()'

  15. 15

    com.android.volley.VolleyError:java.lang.NullPointerException:Attempt to invoke virtual method 'in java.lang.String.length()' on object reference

  16. 16

    com.android.volley.VolleyError:java.lang.NullPointerException:Attempt to invoke virtual method 'in java.lang.String.length()' on object reference

  17. 17

    Service (Android Notification): Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

  18. 18

    Attempt to invoke virtual method 'java.lang.String java.lang.String.toUpperCase()' on a null object reference

  19. 19

    Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference

  20. 20

    'java.lang.String android.content.Context.getPackageName()' on a null object reference

  21. 21

    Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference

  22. 22

    Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference

  23. 23

    Error: Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference

  24. 24

    NullPointerException: Attempt to invoke virtual method 'java.lang.String com.zopim.android.sdk.model.VisitorInfo.getEmail()' app is forced close

  25. 25

    Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference on Volley while executing Swiperefresh Android

  26. 26

    Android: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

  27. 27

    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference

  28. 28

    Exception: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

  29. 29

    Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

HotTag

Archive