尝试在空对象引用上调用Playerview.setplayer的虚拟方法。

huangapple go评论91阅读模式
英文:

Attempt to invoke virtual method on Playerview.setplayer on a null object reference

问题

我在不应该出现空指针异常的地方得到了一个空指针异常。

以下是错误信息输出:

  1. java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'void com.google.android.exoplayer2.ui.PlayerView.setPlayer(com.google.android.exoplayer2.Player)'
  2. at com.example.corbettreportpodcasts.PodcastContentActivity.initializePlayer(PodcastContentActivity.java:40)
  3. at com.example.corbettreportpodcasts.PodcastContentActivity.onStart(PodcastContentActivity.java:55)

以下是活动的源代码(它来自于一个关于ExoPlayer的Google Codelab,并进行了一些修改)。布局文件只是一个带有ID为 video_view 的Playerview元素。

  1. package com.example.corbettreportpodcasts;
  2. import android.annotation.SuppressLint;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import com.google.android.exoplayer2.MediaItem;
  7. import com.google.android.exoplayer2.Player;
  8. import com.google.android.exoplayer2.SimpleExoPlayer;
  9. import com.google.android.exoplayer2.ui.PlayerView;
  10. import com.google.android.exoplayer2.util.Util;
  11. import androidx.appcompat.app.AppCompatActivity;
  12. public class PodcastContentActivity extends AppCompatActivity implements Player.EventListener {
  13. PlayerView playerView;
  14. private SimpleExoPlayer player;
  15. private boolean playWhenReady = true;
  16. private int currentWindow = 0;
  17. private long playbackPosition = 0;
  18. private String source;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. playerView = findViewById(R.id.video_view);
  24. Intent intent = getIntent();
  25. source = intent.getExtras().getString("PODCAST_SOURCE");
  26. }
  27. private void initializePlayer() {
  28. player = new SimpleExoPlayer.Builder(this).build();
  29. playerView.setPlayer(player); // 这里是错误的位置
  30. MediaItem mediaItem = MediaItem.fromUri(source);
  31. player.setMediaItem(mediaItem);
  32. player.setPlayWhenReady(playWhenReady);
  33. player.seekTo(currentWindow, playbackPosition);
  34. player.prepare();
  35. }
  36. // 其他代码...
  37. }

任何帮助都将不胜感激。

这是Codelab的链接 HERE

以下是XML文件的内容:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <com.google.android.exoplayer2.ui.PlayerView
  7. android:id="@+id/video_view"
  8. android:layout_width="0dp"
  9. android:layout_height="wrap_content"
  10. app:layout_constraintEnd_toEndOf="parent"
  11. app:layout_constraintStart_toStartOf="parent"
  12. app:layout_constraintTop_toTopOf="parent">
  13. </com.google.android.exoplayer2.ui.PlayerView>
  14. </androidx.constraintlayout.widget.ConstraintLayout>
英文:

I am getting a null pointer exception where there shouldnt be one.

Here is the error readout

  1. java.lang.NullPointerException: Attempt to invoke virtual method &#39;void com.google.android.exoplayer2.ui.PlayerView.setPlayer(com.google.android.exoplayer2.Player)&#39; on a null object reference
  2. at com.example.corbettreportpodcasts.PodcastContentActivity.initializePlayer(PodcastContentActivity.java:40)
  3. at com.example.corbettreportpodcasts.PodcastContentActivity.onStart(PodcastContentActivity.java:55)

Here is the activity source code (Its from a google codelab on the exoplayer) and has been modified a little. The layout file is just a Playerview element with its ID as video_view

  1. package com.example.corbettreportpodcasts;
  2. import android.annotation.SuppressLint;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import com.google.android.exoplayer2.MediaItem;
  7. import com.google.android.exoplayer2.Player;
  8. import com.google.android.exoplayer2.SimpleExoPlayer;
  9. import com.google.android.exoplayer2.ui.PlayerView;
  10. import com.google.android.exoplayer2.util.Util;
  11. import androidx.appcompat.app.AppCompatActivity;
  12. public class PodcastContentActivity extends AppCompatActivity implements Player.EventListener {
  13. PlayerView playerView;
  14. private SimpleExoPlayer player;
  15. private boolean playWhenReady = true;
  16. private int currentWindow = 0;
  17. private long playbackPosition = 0;
  18. private String source;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. playerView = findViewById(R.id.video_view);
  24. Intent intent = getIntent();
  25. source = intent.getExtras().getString(&quot;PODCAST_SOURCE&quot;);
  26. }
  27. private void initializePlayer() {
  28. player = new SimpleExoPlayer.Builder(this).build();
  29. playerView.setPlayer(player); // HERE IS THE ERROR
  30. MediaItem mediaItem = MediaItem.fromUri(source);
  31. player.setMediaItem(mediaItem);
  32. player.setPlayWhenReady(playWhenReady);
  33. player.seekTo(currentWindow, playbackPosition);
  34. player.prepare();
  35. }
  36. @Override
  37. public void onStart() {
  38. super.onStart();
  39. if (Util.SDK_INT &gt;= 24) {
  40. initializePlayer();
  41. }
  42. }
  43. @Override
  44. public void onResume() {
  45. super.onResume();
  46. hideSystemUi();
  47. if ((Util.SDK_INT &lt; 24 || player == null)) {
  48. initializePlayer();
  49. }
  50. }
  51. @SuppressLint(&quot;InlinedApi&quot;)
  52. private void hideSystemUi() {
  53. playerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
  54. | View.SYSTEM_UI_FLAG_FULLSCREEN
  55. | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  56. | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
  57. | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
  58. | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
  59. }
  60. @Override
  61. public void onPause() {
  62. super.onPause();
  63. if (Util.SDK_INT &lt; 24) {
  64. releasePlayer();
  65. }
  66. }
  67. @Override
  68. public void onStop() {
  69. super.onStop();
  70. if (Util.SDK_INT &gt;= 24) {
  71. releasePlayer();
  72. }
  73. }
  74. private void releasePlayer() {
  75. if (player != null) {
  76. playWhenReady = player.getPlayWhenReady();
  77. playbackPosition = player.getCurrentPosition();
  78. currentWindow = player.getCurrentWindowIndex();
  79. player.release();
  80. player = null;
  81. }
  82. }
  83. }

Any help is appreciated.
This is the codelab HERE

The following is the xml file

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. android:layout_width=&quot;match_parent&quot;
  5. android:layout_height=&quot;match_parent&quot;&gt;
  6. &lt;com.google.android.exoplayer2.ui.PlayerView
  7. android:id=&quot;@+id/video_view&quot;
  8. android:layout_width=&quot;0dp&quot;
  9. android:layout_height=&quot;wrap_content&quot;
  10. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  11. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  12. app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
  13. &lt;/com.google.android.exoplayer2.ui.PlayerView&gt;
  14. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案1

得分: 2

我在错误的布局文件上调用了setContentView,导致findViewByID返回空指针。

英文:

I called setContentView on the wrong layout file leading to findviewbyid giving me a null pointer

huangapple
  • 本文由 发表于 2020年10月11日 05:13:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64298368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定