100

I am working with Fragments which implements an interface.

public class SigninFragment extends Fragment implements SigninInterface 

The interface's method implementation in the fragment class is as follows.

@Override
public void afterSubmitClicked(String userId, Bundle bundle) {

    Log.d(TAG,"Calling time afterSubmitClicked called"+bundle);
    
    if(!userId.equals("-1")){
        //Logged in successfully
        //Move to MusicHome
        
        Intent mIntent = new Intent(getActivity(),MusicHome.class);
        mIntent.putExtra("SigninFragment.user_details", bundle);
        startActivity(mIntent);
        
    }else{
        //Logging in failed
        //show error dialog
    }
    
}

This method is called after exectuting a AsynchronousTask (which extends AsyncTask) class.

But I am getting crash. And the error message shows

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

Logcat

   02-14 16:37:04.648: E/AndroidRuntime(28177): Process: com.raaga.android, PID: 28177
02-14 16:37:04.648: E/AndroidRuntime(28177): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
02-14 16:37:04.648: E/AndroidRuntime(28177):    at android.content.ComponentName.<init>(ComponentName.java:77)
02-14 16:37:04.648: E/AndroidRuntime(28177):    at android.content.Intent.<init>(Intent.java:3996)
02-14 16:37:04.648: E/AndroidRuntime(28177):    at com.raaga.fragments.SigninFragment.afterSubmitClicked(SigninFragment.java:152)
02-14 16:37:04.648: E/AndroidRuntime(28177):    at com.raaga.asynctask.SignInAsyncTask.onPostExecute(SignInAsyncTask.java:92)
02-14 16:37:04.648: E/AndroidRuntime(28177):    at com.raaga.asynctask.SignInAsyncTask.onPostExecute(SignInAsyncTask.java:1)
02-14 16:37:04.648: E/AndroidRuntime(28177):    at android.os.AsyncTask.finish(AsyncTask.java:632)
02-14 16:37:04.648: E/AndroidRuntime(28177):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-14 16:37:04.648: E/AndroidRuntime(28177):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
02-14 16:37:04.648: E/AndroidRuntime(28177):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-14 16:37:04.648: E/AndroidRuntime(28177):    at android.os.Looper.loop(Looper.java:135)
02-14 16:37:04.648: E/AndroidRuntime(28177):    at android.app.ActivityThread.main(ActivityThread.java:5221)
02-14 16:37:04.648: E/AndroidRuntime(28177):    at java.lang.reflect.Method.invoke(Native Method)
02-14 16:37:04.648: E/AndroidRuntime(28177):    at java.lang.reflect.Method.invoke(Method.java:372)
02-14 16:37:04.648: E/AndroidRuntime(28177):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-14 16:37:04.648: E/AndroidRuntime(28177):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Zoe
  • 27,060
  • 21
  • 118
  • 148
Karthikeyan Ve
  • 2,550
  • 4
  • 22
  • 40

18 Answers18

52

I have found the mistake what I did. We need to get the activity instance from the override method OnAttach() For example,

public MainActivity activity;

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

Then pass the activity as context as following.

Intent mIntent = new Intent(activity, MusicHome.class);
Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
Karthikeyan Ve
  • 2,550
  • 4
  • 22
  • 40
49

The answers to this question helped me find my problem, but my source was different, so hopefully this can shed light on someone finding this page searching for answers to the 'random' context crash:

I had specified a SharedPreferences object, and tried to instantiate it at it's class-level declaration, like so:

public class MyFragment extends FragmentActivity {
    private SharedPreferences sharedPref =
        PreferenceManager.getDefaultSharedPreferences(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //...

Referencing this before the onCreate caused the "java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference" error for me.

Instantiating the object inside the onCreate() solved my problem, like so:

public class MyFragment extends FragmentActivity {
    private SharedPreferences sharedPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //...
        sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

Hope that helps.

mix3d
  • 4,122
  • 2
  • 25
  • 47
  • 1
    I had the same problem while calling getDefaultSharedPreferences() from Application class constructor (public class MyApplication extends Application). Your message helped to see that I should move it to the onCreate method. – OlivierGrenoble Aug 29 '18 at 08:27
  • 1
    Nice tip, helped me a lot – Allexandre S. Oct 01 '20 at 00:26
25

android API level 23.

Use

getContext()

instead of

getActivity()

getActivity() gives the parent Activity, a Context object

Here You can use

MyActivity.this

instead of

getActivity() / getApplicationContext()

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
sivaBE35
  • 1,876
  • 18
  • 23
11

I had the same problem trying to show a Toast in a fragment.

After some debugging I found out that I was removing the fragment before calling:

Toast.makeText(getContext(), "text", Toast.LENGTH_SHORT).show();

Because the fragment had been removed, the context became null, causing the exception.

Simple solution: call the getContext() before removing the fragment.

vezunchik
  • 3,669
  • 3
  • 16
  • 25
renatoarg
  • 303
  • 4
  • 13
10

In my case the error occurred inside a Fragment on this line:

Intent intent = new Intent(getActivity(), SecondaryActivity.class);

It happened when I double clicked on an item which triggered the code above so two SecondaryActivity.class activities were launched at the same time, one on top of the other. I closed the top SecondaryActivity.class activity by pressing back button which triggered a call to getActivity() in the SecondaryActivity.class which came to foreground. The call to getActivity() returned null. It's some kind of weird Android bug so it usually should not happen. You can block the clicks after the user clicked once.

vovahost
  • 34,185
  • 17
  • 113
  • 116
6

In my case I had a onCLick method in a recyclerview adapter,Context context; at the beginning.

 holder.cTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, StoryActivity.class);
                    intent.putExtra("STORY", mComments.get(position));
                    context.startActivity(intent);
                }
            });

And i changed to get the context from the current view

 holder.cTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(v.getContext(), StoryActivity.class);
                    intent.putExtra("STORY", mComments.get(position));
                    v.getContext().startActivity(intent);
                }
            });
Mihai
  • 26,325
  • 7
  • 66
  • 81
4

For me the problem was that I was passing Activity to the constructor, not Context

public Adapter(Activity activity, List<MediaItem> items, boolean can) {
    mItems = items;
    canEdit = can;
    mActivity = activity;
}

and using this activity to getDefaultSharedPreferences(), so I changed the Activity to Context and I was still calling the Adapter constructor with MainActivity.this

SpyZip
  • 5,511
  • 4
  • 32
  • 53
3

You only need to do this:

Intent myIntent = new Intent(MainActivity.this, nextActivity.class);
josliber
  • 43,891
  • 12
  • 98
  • 133
2

My class is not extends to Activiti. I solved the problem this way.

class MyOnBindViewHolder : LogicViewAdapterModel.LogicAdapter {
          ...
 holder.title.setOnClickListener({v->
                v.context.startActivity(Intent(context,  HomeActivity::class.java))
            })
          ...
    }
Segrei Ulanov
  • 141
  • 1
  • 4
2

Put fragment name before the activity

Intent mIntent = new Intent(SigninFragment.this.getActivity(),MusicHome.class);
ARAVIND RAJ
  • 486
  • 5
  • 11
1

You solve the issue with a try/ catch. This crash happens when user close the app before the start intent.

try
{
    Intent mIntent = new Intent(getActivity(),MusicHome.class);
    mIntent.putExtra("SigninFragment.user_details", bundle);
    startActivity(mIntent);
}
catch (Exception e) {
    e.printStackTrace();
}
Edwin
  • 1,135
  • 2
  • 16
  • 24
  • Great answer. This was exactly what was happening to me. I had a countdowntimer that would create an intent in the `onFinish` method. But if the user pressed the back button to go to the previous activity where the timer was not created, then I would get a null object reference since getActivity() would no longer work. This could also be useful for threads that weren't closed after the user left the activity. Thanks a bunch! – Chris Gong Nov 19 '17 at 19:15
0

You can solve your problem easily and use your activity anywhere in the activity, just store activity like this:

public class MainActivity extends AppCompatActivity{
      Context context = this;

      protected void onCreate(Bundle savedInstanceState) {....}

      AnotherClass {
          Intent intent = new Intent(context, ExampleActivity.class);
          intent.putExtra("key", "value");
          startActivity(intent);
      }

}
igg
  • 2,172
  • 3
  • 10
  • 33
0

Due to onAttach is deprecated in API23 and above... In my Fragment, I declare and set parentActivity before hand everytime when I go into Fragment. Most likely happen due to when we pressed back button caused the context to become null. Therefore below is my solution.

private Activity parentActivity;

public void onStart(){
        super.onStart();
        parentActivity = getActivity();
//...

}
0
  public MessageAdapter(Context context, List<Messages> mMessageList) {

    this.mContext = context;
    this.mMessageList = mMessageList;

  }
0

Use can use in global

Context context;

and create a constructor and passing the context. LoginClass is name of the class

LoginClass(Context context)
{this.context=context;}

and also when we call the class then use getApplicationContext like LoginClass lclass = new LoginClass(getApplicationContext) thats it.

0

Sometimes startActivity method throws an exception like especially in Fragments:

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

So you should intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) method

Samir Alakbarov
  • 1,120
  • 11
  • 21
0

If this error happened when getting context , is means you are remove the context before calling it. Lets say if you are willing to get Toast in the fragment and you remove the fragment before call the Toast.

Toast.makeText(context, "Permission Denied", Toast.LENGTH_SHORT).show()

So solution is either wait to Toast happens to remove or change the fragment, or remove the Toast or any thing that needs to context before change or remove the fragment.

Dinith
  • 839
  • 14
  • 22
-1

In Kotlin

lateinit var views:View?
override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
     views=inflater.inflate(R.layout.fragment_dashboard, container, false)
   Toast.makeText(views.context ,"hello",Toast.LENGTH_LONG).show()
   return views;
}