Skip to content
Advertisement

Attempt to invoke virtual method ‘android.view.View com.google.android.exoplayer2.ui.PlayerView.findViewById(int)’ on a null object reference

the error is here: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘android.view.View com.google.android.exoplayer2.ui.PlayerView.findViewById(int)’ on a null object reference at com.wk.videoplayer.PlayerActivity.onCreate(PlayerActivity.java:32)

Here is the XML code of activity_player.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PlayerActivity">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/exoplayer_movie"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"
        app:controller_layout_id="@layout/custom_controller_view"
        app:fastforward_increment="10000"
        app:hide_on_touch="true"
        app:player_layout_id="@layout/exo_simple_player_view"
        app:resize_mode="fit"
        app:rewind_increment="10000"
        app:show_timeout="5000"
        app:shutter_background_color="@color/black"
        app:use_controller="true" />

</LinearLayout>

and here is the PlayerActivity.java code:

    PlayerView playerView;
    SimpleExoPlayer simpleExoPlayer;
    int position = -1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_player);
        playerView.findViewById(R.id.exoplayer_movie);
        position = getIntent().getIntExtra("position", -1);
        String path = videoFiles.get(position).getPath();
        if (path != null)
        {
            Uri uri = Uri.parse(path);
            simpleExoPlayer = new SimpleExoPlayer.Builder(this)
                    .build();
            DataSource.Factory factory = new DefaultDataSourceFactory(this,
                    Util.getUserAgent(this, "MyAPPName"));
            ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
            MediaSource mediaSource = new ProgressiveMediaSource
                    .Factory(factory,extractorsFactory).createMediaSource(uri);
            playerView.setPlayer(simpleExoPlayer);
            playerView.setKeepScreenOn(true);
            simpleExoPlayer.prepare(mediaSource);
            simpleExoPlayer.setPlayWhenReady(true);
        }
    }

please help me solve this error, I have already searched on stakoverflow for its solutions but I am not able to fix it please help me fix it.

Advertisement

Answer

very simple!!! change this code:

playerView.findViewById(R.id.exoplayer_movie);

to:

playerView=findViewById(R.id.exoplayer_movie);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement