3

i need help. I'm in the finish phase of development of my android application and now after a few month of develop the app magically crash with this error :

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

I don't know what is the problem but i will post the code that cause the crash :

   public static void start(Context context) {
        context.startActivity(new Intent(context, ConversationsActivity.class));
    }

called with this piece of code (inside a fragment):

ConversationsActivity.start(getActivity());

The last change that i've make was to add Fabric.io ( in particular Branch ) and from the history in the last commit on git i don't show nothing that can produce this error

EDIT :

 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:4900)
at com.exampleapp.views.messages.ConversationsActivity.start(ConversationsActivity.java:31)
at com.exampleapp.views.menu.MenuFragment.onMenuMessagesClicked(MenuFragment.java:116)
at com.exampleapp.views.menu.MenuFragment_ViewBinding$5.doClick(MenuFragment_ViewBinding.java:82)
at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
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:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
4
  • Please edit your question and post the entire Java stack trace. Commented Mar 23, 2017 at 21:20
  • i've update the question Commented Mar 23, 2017 at 21:26
  • getActivity() appears to be returning null. I am not completely certain why. Commented Mar 23, 2017 at 21:29
  • yes , now i show that also in other parts of code the context/activity retrieved with getContext() or getActivity() result null. Commented Mar 23, 2017 at 21:51

3 Answers 3

3

Or you could do this:

public static void start(Activity activity) {
    activity.startActivity(new Intent(activity, ConversationsActivity.class));
}
Sign up to request clarification or add additional context in comments.

Comments

3

You should use getActivity() to launch an Activity from Fragment.

From a Fragment: Context is parent activity (getActivity()).

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

From an Activity: Context is current activity (this).

Intent intent = new Intent(this, ConversationsActivity.class);
startActivity(intent);

Comments

1

Your problem is that you are using the intent wrong. Replace your start() code with this:

public static void start() {
    Intent i = new Intent(getApplicationContext(), ConversationsActivity.class);
    startActivity(i);
}

Then instead of using ConversationsActivity.start(getActivity()); to call it, just use start(); when you want to call the method.

Hope this helps!

3 Comments

thanks for the answer but i don't think this is the solution , also because it has always worked. Why ApplicationContext?
Just try this, it always works for me. Application Context is always a safe choice
nothing to do man :( but now i realised that the same code work with different activity called from other fragment O.o i have no idea about what happened

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.