尝试在空对象引用上调用虚拟方法'android.view.View com.google.android.exoplayer2.ui.PlayerView.findViewById(int)'



错误如下:由:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.View com.google.android.exoplayer2.ui.PlayerView.findViewById(int)'引起在com.wk.videoplayer.PlayerActivity.onCreate (PlayerActivity.java: 32)

下面是activity_player.xml的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>

这是PlayerActivity.java代码:

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);
}
}

请帮助我解决这个错误,我已经在stakoverflow上搜索了它的解决方案,但我无法修复它,请帮助我修复它。

你需要在你的活动上调用findViewById(),而不是在你的playerView对象上

试试这样做:

playerView = this.findViewById(R.id.exoplayer_movie);

或者只是

playerView = findViewById(R.id.exoplayer_movie);

很简单!!修改代码:

playerView.findViewById(R.id.exoplayer_movie);

:

playerView=findViewById(R.id.exoplayer_movie);

最新更新