-1
Intent intent =new Intent(getActivity(),LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
startActivity(intent);

//This is the error I receive//

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.salart.applogin, PID: 11946
    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:130)
        at android.content.Intent.<init>(Intent.java:6347)
        at com.example.salart.applogin.OneFragment$2.onAuthStateChanged(OneFragment.java:78)
        at com.google.firebase.auth.zzl.run(Unknown Source:3)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at com.google.android.gms.internal.firebase_auth.zzf.dispatchMessage(Unknown Source:6)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Chirag
  • 56,621
  • 29
  • 151
  • 198
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – ADM Dec 06 '18 at 09:43
  • Probably `getActivity()` is returning null -> `Fragment` is no longer attached. – ADM Dec 06 '18 at 09:45
  • refer this link : https://stackoverflow.com/questions/25479578/how-to-switch-from-fragment-to-activity – Ali Dec 06 '18 at 09:48
  • put a null check and make sure your getActivity is null, if it is null then keep Activity reference in onAttach(Context context) – sohaib karim Dec 06 '18 at 09:49
  • Can you provide the whole function of the OnClickListener of your button? – procra Dec 06 '18 at 09:50

2 Answers2

0

CALLING FROM AN Adapter THAT BELONGS TO THE FRAGMENT

holder.demoView.setOnClickListener(v -> { Intent intent = new Intent();
                    /*Gson gson = new Gson();
                    Type type = new TypeToken<List<SampleModel>>(){}.getType();
                    String data = gson.toJson(parsable, type);
                    intent.putExtra("chat_data", data);*/ 
//ignore the commented above code, else if only you want something like this to parse data to the called activity.

      intent.setClass(v.getContext(), CallingActivity.class);
                        v.getContext().startActivity(intent);
 });

CALLING FROM THE OnCreateView WITHIN THE FRAGMENT

    Intent intent =new Intent(getActivity(), CallingActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
                getActivity().startActivity(intent);

Take note of v.getContext().startActivity(intent); this one have never failed me!!! so if you're calling the activity from a click, then just use the view.getContext.startActivity(intent);

Wale
  • 1,644
  • 15
  • 31
0

try replacing

startActivity(intent);

with

getActivity().startActivity(intent);
zaheer ahmed
  • 168
  • 1
  • 2
  • 11