-1

i'm having this error in Android versions 4.+, (specially in 4.1 and 4.3) while not in any other versions of android. The Activities and libraries (i use GooglePlayServices and YouTubePlayerAPI) are referenced in Manifest.xml file and in gradle file.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.test/com.app.test.DetailActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.app.test.content.FirstFragment.onCreate(FirstFragment.java:151)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1912)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:266)
at com.app.test.DetailActivity.onCreate(DetailActivity.java:73)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more

The code from fragment where the error is caused (line 151) is the onCreate() method of the fragment attached:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

        if(getArguments() != null && getArguments().containsKey(ARG_ITEM_ID)) {
            // Loads the content specified by the fragment arguments.
            mItem = Items.getInstance().getMap().get(getArguments().getString(ARG_ITEM_ID));
        }

        // Initialize mCustomMediaPlayer
        mCustomMediaPlayer = new CustomMediaPlayer(getActivity().getApplicationContext(), mItem.getTrackId());
    }

Where pointed line 151 is:

mCustomMediaPlayer = new CustomMediaPlayer(VillancicosActivity.getContext(), mItem.getTrackId());

CustomMediaPlayer Class:

public class CustomMediaPlayer {

    /////// Constants

    private static final String TAG = "CustomMediaPlayer";

    /////// Fields

    /**
     * The application context.
     * Is used to manage MediaPlayer and async class.
     */
    private Context mContext;

    /**
     * MediaPlayer instance.
     */
    private MediaPlayer mMediaPlayer;

    /**
     * The id representing track used in MediaPlayer create() method
     * where will be played.
     */
    private Integer mTrackId;

    /**
     * A flag indicating if the track is playing or not.
     */
    private Boolean mIsPlaying = false;

    /**
     * Initialize providing the application context and the calling fragment track id.
     *
     * @param context The Context to use.
     * @param trackId The raw resource id to use as datasource.
     */
    public CustomMediaPlayer(Context context, Integer trackId) {
        mContext = context;
        mTrackId = trackId;
    }

.....
}

This is part of the class, where the Main Constructor is called from FirstFragment in line 151.

Any idea? Thanks everyone!

GmloMalo
  • 667
  • 7
  • 14

2 Answers2

1

try this

mCustomMediaPlayer = new CustomMediaPlayer(getActivity(), mItem.getTrackId());

and check mItem ==null or not.

M D
  • 47,665
  • 9
  • 93
  • 114
  • I' ve tried it and mItem is null in some devices with android 2.3.3 and 2.3.7 but i don't know what causes it – GmloMalo Dec 22 '14 at 23:05
0

your activity does not recognize Villancicos Activity.getContext() you need to pass a valid context. try getApplicationContext() instead.

Maor Hadad
  • 1,850
  • 21
  • 36