英文:
Attempt to invoke virtual method on Playerview.setplayer on a null object reference
问题
我在不应该出现空指针异常的地方得到了一个空指针异常。
以下是错误信息输出:
java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'void com.google.android.exoplayer2.ui.PlayerView.setPlayer(com.google.android.exoplayer2.Player)'
at com.example.corbettreportpodcasts.PodcastContentActivity.initializePlayer(PodcastContentActivity.java:40)
at com.example.corbettreportpodcasts.PodcastContentActivity.onStart(PodcastContentActivity.java:55)
以下是活动的源代码(它来自于一个关于ExoPlayer的Google Codelab,并进行了一些修改)。布局文件只是一个带有ID为 video_view 的Playerview元素。
package com.example.corbettreportpodcasts;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.util.Util;
import androidx.appcompat.app.AppCompatActivity;
public class PodcastContentActivity extends AppCompatActivity implements Player.EventListener {
PlayerView playerView;
private SimpleExoPlayer player;
private boolean playWhenReady = true;
private int currentWindow = 0;
private long playbackPosition = 0;
private String source;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playerView = findViewById(R.id.video_view);
Intent intent = getIntent();
source = intent.getExtras().getString("PODCAST_SOURCE");
}
private void initializePlayer() {
player = new SimpleExoPlayer.Builder(this).build();
playerView.setPlayer(player); // 这里是错误的位置
MediaItem mediaItem = MediaItem.fromUri(source);
player.setMediaItem(mediaItem);
player.setPlayWhenReady(playWhenReady);
player.seekTo(currentWindow, playbackPosition);
player.prepare();
}
// 其他代码...
}
任何帮助都将不胜感激。
这是Codelab的链接 HERE
以下是XML文件的内容:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/video_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</com.google.android.exoplayer2.ui.PlayerView>
</androidx.constraintlayout.widget.ConstraintLayout>
英文:
I am getting a null pointer exception where there shouldnt be one.
Here is the error readout
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.exoplayer2.ui.PlayerView.setPlayer(com.google.android.exoplayer2.Player)' on a null object reference
at com.example.corbettreportpodcasts.PodcastContentActivity.initializePlayer(PodcastContentActivity.java:40)
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
package com.example.corbettreportpodcasts;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.util.Util;
import androidx.appcompat.app.AppCompatActivity;
public class PodcastContentActivity extends AppCompatActivity implements Player.EventListener {
PlayerView playerView;
private SimpleExoPlayer player;
private boolean playWhenReady = true;
private int currentWindow = 0;
private long playbackPosition = 0;
private String source;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playerView = findViewById(R.id.video_view);
Intent intent = getIntent();
source = intent.getExtras().getString("PODCAST_SOURCE");
}
private void initializePlayer() {
player = new SimpleExoPlayer.Builder(this).build();
playerView.setPlayer(player); // HERE IS THE ERROR
MediaItem mediaItem = MediaItem.fromUri(source);
player.setMediaItem(mediaItem);
player.setPlayWhenReady(playWhenReady);
player.seekTo(currentWindow, playbackPosition);
player.prepare();
}
@Override
public void onStart() {
super.onStart();
if (Util.SDK_INT >= 24) {
initializePlayer();
}
}
@Override
public void onResume() {
super.onResume();
hideSystemUi();
if ((Util.SDK_INT < 24 || player == null)) {
initializePlayer();
}
}
@SuppressLint("InlinedApi")
private void hideSystemUi() {
playerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
@Override
public void onPause() {
super.onPause();
if (Util.SDK_INT < 24) {
releasePlayer();
}
}
@Override
public void onStop() {
super.onStop();
if (Util.SDK_INT >= 24) {
releasePlayer();
}
}
private void releasePlayer() {
if (player != null) {
playWhenReady = player.getPlayWhenReady();
playbackPosition = player.getCurrentPosition();
currentWindow = player.getCurrentWindowIndex();
player.release();
player = null;
}
}
}
Any help is appreciated.
This is the codelab HERE
The following is the xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/video_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</com.google.android.exoplayer2.ui.PlayerView>
</androidx.constraintlayout.widget.ConstraintLayout>
答案1
得分: 2
我在错误的布局文件上调用了setContentView,导致findViewByID返回空指针。
英文:
I called setContentView on the wrong layout file leading to findviewbyid giving me a null pointer
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论