简体   繁体   中英

Getting a NullPointerException when trying to initialize streaming using ExoPlayer?

The app I'm working on uses the generic media player or the Nex Player to play its video files. My task is to have it use the Google ExoPlayer instead which I have been working on implementing. However, I'm unable to initialize the ExoPlayer class due to a NullPointerException that is occuring in the Util class that is part of the exoplayer2 package.

The following method was already implemented in my code and was used to initialize the native player and the nexplayer.

private void onLaunchStreamingSuccess() {
    final Context context = MediaDetailsStageButtons.this.getContext();
    final Intent intent = new Intent(context, ExoStreamingPlaybackScreen.class);
    intent.putExtra( AbstractMediaDetailsScreen.EXTRA_MEDIA_ID, MediaDetailsStageButtons.this.mMedia.getID() );
    context.startActivity(intent);
}

After startActivity is called, the app crashes with the following log statements

11-09 11:45:10.163 27482 27482 E QewErrorReporter: Entered uncaughtException [caught Exception: Unable to instantiate activity ComponentInfo{com.directv.application.android.go.production/com.morega.qew.ui.streaming.ExoStreamingPlaybackScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference]
11-09 11:45:10.163 27482 27482 E QewErrorReporter: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.directv.application.android.go.production/com.morega.qew.ui.streaming.ExoStreamingPlaybackScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3096)
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at android.app.ActivityThread.access$1100(ActivityThread.java:223)
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at android.os.Handler.dispatchMessage(Handler.java:102)
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at android.os.Looper.loop(Looper.java:158)
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at android.app.ActivityThread.main(ActivityThread.java:7231)
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at java.lang.reflect.Method.invoke(Native Method)
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
11-09 11:45:10.163 27482 27482 E QewErrorReporter: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:97)
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at com.google.android.exoplayer2.util.Util.getUserAgent(Util.java:676)
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at com.morega.qew.ui.streaming.ExoStreamingPlaybackScreen.<init>(ExoStreamingPlaybackScreen.java:88)
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at java.lang.Class.newInstance(Native Method)
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at android.app.Instrumentation.newActivity(Instrumentation.java:1096)
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3086)
11-09 11:45:10.163 27482 27482 E QewErrorReporter:  ... 9 more

Going into the Util class, the method 'getUserAgent' is where the problem is occurring, specifically at the second line of the try block.

public static String getUserAgent(Context context, String applicationName) {
String versionName;
try {
  String packageName = context.getPackageName();
  PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
  versionName = info.versionName;
} catch (NameNotFoundException e) {
  versionName = "?";
}
return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE
    + ") " + "ExoPlayerLib/" + ExoPlayerLibraryInfo.VERSION;}

It might just be my inexperience with Android which is why I'm unable to solve this issue but I would love some help on what I can do to fix it.

What call is being made at line 88 in ExoStreamingPlaybackScreen.java ? It looks like that is where the crash is happening.

It might be that the Context you are passing in there is null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM