为什么当我执行调用另一个类中的另一个过程的JavascriptInterface过程时,我的上下文变为null?
关于使用Javascript接口从另一个类调用过程,我遇到了一个问题。 问题是,当我从ExoPlayer类调用IniexPlayer时,它会给我NullPointerException 正如我在日志中看到的,我发现这是因为我发送到iniExoPlayer过程的上下文为空 我已经尝试在ExoPlayer类中使用为什么当我执行调用另一个类中的另一个过程的JavascriptInterface过程时,我的上下文变为null?,javascript,java,android,webview,Javascript,Java,Android,Webview,关于使用Javascript接口从另一个类调用过程,我遇到了一个问题。 问题是,当我从ExoPlayer类调用IniexPlayer时,它会给我NullPointerException 正如我在日志中看到的,我发现这是因为我发送到iniExoPlayer过程的上下文为空 我已经尝试在ExoPlayer类中使用context.getActivity(),但仍然收到错误 你知道我该怎么解决这个问题吗 WebAppInterface.java 公共类WebAppInterface{ 公共语境; 公共W
context.getActivity()
,但仍然收到错误
你知道我该怎么解决这个问题吗
WebAppInterface.java
公共类WebAppInterface{
公共语境;
公共WebAppInterface(上下文){
this.context=上下文;
}
ExoPlayer ExoPlayer=新的ExoPlayer();
@JavascriptInterface
公共无效显示(字符串视频){
exoPlayer.iniExoPlayer(视频播放器);
}
ExoPlayer.java
公共类ExoPlayer扩展AppCompative活动{
私人PlayerView PlayerView;
私人SimpleExoPlayer SimpleExoPlayer;
公共静态字符串url;
公共语境;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.video_播放器);
}
公共播放器(字符串url){
Log.d(“owo”,url);
simpleExoPlayer=ExoPlayerFactory.newSimpleInstance(上下文);
playerView=findviewbyd(R.id.video\u播放器);
playerView.setPlayer(simpleExoPlayer);
DataSource.Factory dataSourceFactory=newdefaultdatasourcefactory(context,Util.getUserAgent(context,appname));
MediaSource videoSource=new extractoremiasource.Factory(dataSourceFactory.createMediaSource(Uri.parse(url));
simpleExoPlayer.prepare(视频源);
simpleExoPlayer.setplaywhenrady(true);
}
Logcat
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at com.google.android.exoplayer2.video.MediaCodecVideoRenderer.<init>(MediaCodecVideoRenderer.java:226)
W/System.err: at com.google.android.exoplayer2.DefaultRenderersFactory.buildVideoRenderers(DefaultRenderersFactory.java:211)
at com.google.android.exoplayer2.DefaultRenderersFactory.createRenderers(DefaultRenderersFactory.java:180)
at com.google.android.exoplayer2.SimpleExoPlayer.<init>(SimpleExoPlayer.java:213)
at com.google.android.exoplayer2.SimpleExoPlayer.<init>(SimpleExoPlayer.java:166)
at com.google.android.exoplayer2.ExoPlayerFactory.newSimpleInstance(ExoPlayerFactory.java:386)
at com.google.android.exoplayer2.ExoPlayerFactory.newSimpleInstance(ExoPlayerFactory.java:352)
at com.google.android.exoplayer2.ExoPlayerFactory.newSimpleInstance(ExoPlayerFactory.java:320)
at com.google.android.exoplayer2.ExoPlayerFactory.newSimpleInstance(ExoPlayerFactory.java:214)
at com.google.android.exoplayer2.ExoPlayerFactory.newSimpleInstance(ExoPlayerFactory.java:180)
at com.google.android.exoplayer2.ExoPlayerFactory.newSimpleInstance(ExoPlayerFactory.java:151)
at com.google.android.exoplayer2.ExoPlayerFactory.newSimpleInstance(ExoPlayerFactory.java:141)
at adnankhairi.webviewer2.ExoPlayer.iniExoPlayer(ExoPlayer.java:37)
at adnankhairi.webviewer2.WebAppInterface.show(WebAppInterface.java:29)
at android.os.MessageQueue.nativePollOnce(Native Method)
W/System.err: at android.os.MessageQueue.next(MessageQueue.java:328)
at android.os.Looper.loop(Looper.java:164)
at android.os.HandlerThread.run(HandlerThread.java:61)
I/chromium: [INFO:CONSOLE(55)] "Uncaught Error: Java exception was raised during method invocation", source: (55)
W/System.err:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.Context android.content.Context.getApplicationContext()”
位于com.google.android.exoplayer2.video.MediaCodeVideoRenderer.(MediaCodeVideoRenderer.java:226)
W/System.err:com.google.android.exoplayer2.DefaultRendersFactory.buildVideoRenders(DefaultRendersFactory.java:211)
位于com.google.android.exoplayer2.DefaultRendersFactory.CreateRenders(DefaultRendersFactory.java:180)
位于com.google.android.exoplayer2.SimpleExoPlayer。(SimpleExoPlayer.java:213)
位于com.google.android.exoplayer2.SimpleExoPlayer。(SimpleExoPlayer.java:166)
在com.google.android.exoplayer2.ExoPlayerFactory.newSimpleInstance(ExoPlayerFactory.java:386)
在com.google.android.exoplayer2.ExoPlayerFactory.newSimpleInstance(ExoPlayerFactory.java:352)上
在com.google.android.exoplayer2.ExoPlayerFactory.newSimpleInstance(ExoPlayerFactory.java:320)上
在com.google.android.exoplayer2.ExoPlayerFactory.newSimpleInstance(ExoPlayerFactory.java:214)上
在com.google.android.exoplayer2.ExoPlayerFactory.newSimpleInstance(ExoPlayerFactory.java:180)上
在com.google.android.exoplayer2.ExoPlayerFactory.newSimpleInstance(ExoPlayerFactory.java:151)上
在com.google.android.exoplayer2.ExoPlayerFactory.newSimpleInstance(ExoPlayerFactory.java:141)上
在adnankhairi.webviewer2.ExoPlayer.iniExoPlayer(ExoPlayer.java:37)
位于adnankhairi.webviewer2.WebAppInterface.show(WebAppInterface.java:29)
在android.os.MessageQueue.nativePollOnce(本机方法)
W/System.err:at-android.os.MessageQueue.next(MessageQueue.java:328)
位于android.os.Looper.loop(Looper.java:164)
运行(HandlerThread.java:61)
I/chromium:[INFO:CONSOLE(55)]“未捕获错误:在方法调用期间引发了Java异常”,来源:(55)
ExoPlayer ExoPlayer=新的ExoPlayer()
–除了您从不在ExoPlayer
中分配公共上下文;
字段之外,您不能自己实例化活动
类并让它们正常工作。基本上下文
将永远不会被设置–这将导致进一步的空点异常
s–lifecCycle方法将永远不会运行,因此将没有要查找的视图
s,其上是findViewById()
本身会抛出自己的异常,等等。不清楚您想要的总体设计是什么,但这是当前问题的原因。因此,我应该在ExoPlayer中启动这个。上下文也会有帮助吗?正如我所说,直接实例化ExoPlayer
是您当前问题的原因。即使您添加了this.context=this
,你仍然会得到一个NullPointerException
。我不清楚你到底想做什么,但是如果你只是想从WebAppInterface
启动你的ExoPlayer
,那么你应该使用startActivity()
,并将您的字符串视频传递给此处使用的Intent
;例如,总共,context.startActivity(新Intent(context,ExoPlayer.class).putExtra(“video”,vidio));
。然后,在ExoPlayer
的onCreate()
方法中,字符串视频=getIntent().getStringExtra(“视频”)
,iniExoPlayer(视频);
。跟我来吗?好的,它解决了空指针问题…也许你可以在回答部分发布这个问题,并解释代码?