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

问题描述

我收到了一个不应该存在的空指针异常。

这是错误读数

 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)

这里是活动源代码(来自exoplayer上的google codelab),并已进行了一些修改。布局文件只是一个Playerview元素,其ID为 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;
        }
    }
}

感谢您的帮助。 这是代码实验室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>

解决方法

我在错误的布局文件上调用了setContentView,导致findviewbyid给了我空指针

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...