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

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

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 &#39;void com.google.android.exoplayer2.ui.PlayerView.setPlayer(com.google.android.exoplayer2.Player)&#39; 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(&quot;PODCAST_SOURCE&quot;);

    }

    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 &gt;= 24) {
            initializePlayer();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        hideSystemUi();
        if ((Util.SDK_INT &lt; 24 || player == null)) {
            initializePlayer();
        }
    }

    @SuppressLint(&quot;InlinedApi&quot;)
    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 &lt; 24) {
            releasePlayer();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (Util.SDK_INT &gt;= 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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;com.google.android.exoplayer2.ui.PlayerView
        android:id=&quot;@+id/video_view&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;


    &lt;/com.google.android.exoplayer2.ui.PlayerView&gt;

&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:

确定