android.content.Context.getPackageName()’ on a null object reference

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

Leave a Comment