尝试使用 ExoPlayer 初始化流时出现 NullPointerException?
Posted
技术标签:
【中文标题】尝试使用 ExoPlayer 初始化流时出现 NullPointerException?【英文标题】:Getting a NullPointerException when trying to initialize streaming using ExoPlayer? 【发布时间】:2016-11-09 18:44:06 【问题描述】:我正在开发的应用程序使用通用媒体播放器或 Nex Player 播放其视频文件。我的任务是让它使用我一直在努力实施的 Google ExoPlayer。但是,由于作为 exoplayer2 包的一部分的 Util 类中发生 NullPointerException,我无法初始化 ExoPlayer 类。
下面的方法已经在我的代码中实现了,用来初始化native player和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);
调用 startActivity 后,应用程序崩溃并显示以下日志语句
11-09 11:45:10.163 27482 27482 E QewErrorReporter: Entered uncaughtException [caught Exception: Unable to instantiate activity ComponentInfocom.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 ComponentInfocom.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
进入 Util 类,方法“getUserAgent”是问题发生的地方,特别是在 try 块的第二行。
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;
这可能只是我对 Android 的缺乏经验,这就是我无法解决此问题的原因,但我希望能得到一些帮助来解决这个问题。
【问题讨论】:
【参考方案1】:line 88
中的ExoStreamingPlaybackScreen.java
正在拨打什么电话?看起来这就是崩溃发生的地方。
你传入的Context
可能是null
。
【讨论】:
该行是'private String userAgent = Util.getUserAgent(this, "");'但它是在初始字段声明中。我现在对其进行了更改,以便在 onCreate 中对其进行初始化,这似乎已经解决了这个问题。非常感谢,下次发问题之前我会更加努力。以上是关于尝试使用 ExoPlayer 初始化流时出现 NullPointerException?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 android 中实现 Exoplayer 2.11.1?