膨胀类 com.google.android.exoplayer2.ui.SimpleExoPlayerView 时出错

Posted

技术标签:

【中文标题】膨胀类 com.google.android.exoplayer2.ui.SimpleExoPlayerView 时出错【英文标题】:Error inflating class com.google.android.exoplayer2.ui.SimpleExoPlayerView 【发布时间】:2018-09-07 01:15:42 【问题描述】:

一件奇怪的事情:代码在使用模拟器时运行良好,但在运行构建未签名的 apk 时崩溃。

当连接到android studio时,出现如下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfocom.tv2z.demo/com.tv2z.demo.DemoActivity: android.view.InflateException: Binary XML file line #32: Binary XML file line #32: Error inflating class com.google.android.exoplayer2.ui.SimpleExoPlayerView
                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                Caused by: android.view.InflateException: Binary XML file line #32: Binary XML file line #32: Error inflating class com.google.android.exoplayer2.ui.SimpleExoPlayerView
                                                                Caused by: android.view.InflateException: Binary XML file line #32: Error inflating class com.google.android.exoplayer2.ui.SimpleExoPlayerView
                                                                Caused by: java.lang.reflect.InvocationTargetException
                                                                   at java.lang.reflect.Constructor.newInstance0(Native Method)
                                                                   at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
                                                                   at android.view.LayoutInflater.createView(LayoutInflater.java:645)
                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                                   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                                   at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                                                                   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                   at com.tv2z.demo.DemoActivity.onCreate(***DemoActivity.java:80***)
                                                                   at android.app.Activity.performCreate(Activity.java:6679)
                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                Caused by: java.lang.NoSuchFieldError: No field SimpleExoPlayerView_shutter_background_color of type I in class Lcom/google/android/exoplayer2/ui/R$styleable; or its superclasses (declaration of 'com.google.android.exoplayer2.ui.R$styleable' appears in /data/app/com.tv2z.demo-1/base.apk:classes9.dex)
                                                                   at com.google.android.exoplayer2.ui.SimpleExoPlayerView.<init>(SourceFile:274)
                                                                   at com.google.android.exoplayer2.ui.SimpleExoPlayerView.<init>(SourceFile:233)

这是我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".DemoActivity">

<TextView
android:id="@+id/sample_app_title"
android:text="ExoPlayer 2 Example App:"
android:layout_
android:layout_
android:textSize="30sp"
android:textColor="#000000"
android:gravity="center_horizontal"/>

<TextView
android:id="@+id/resolution_textView"
android:layout_
android:layout_
android:text="Resolution"
android:textSize="30px"
android:background="#FFFFFF"
android:textColor="#000000"
android:layout_below="@+id/sample_app_title"
android:gravity="center_horizontal"/>

<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/exo_player_view"
android:focusable="true"
android:layout_
android:layout_
android:layout_below="@+id/resolution_textView"
android:layout_marginTop="2dp" />
</RelativeLayout>

这是我的 DemoActivity 文件: 公共类 DemoActivity 扩展 AppCompatActivity 实现 View.OnClickListener,PlaybackControlView.VisibilityListener

private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
private static final String TAG = "PlayerActivity";
private SimpleExoPlayer player;
private SimpleExoPlayerView playerView;
String videoURL = "http://www.sample-videos.com/video/mp4/360/big_buck_bunny_360p_30mb.mp4";
private long playbackPosition;
private int currentWindow;
private long playerPosition;
private boolean isTimelineStatic;
private int playerWindow;
private Timeline.Window window;
private boolean shouldAutoPlay;
private CustPlugin plugin;
public Tracker tracker;
public MediaDetails medDetails;
private EventLogger eventLogger;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    shouldAutoPlay = false; // make it false to stop autoplay
    setContentView(R.layout.activity_demo);
    ButterKnife.bind(this);
    window = new Timeline.Window();
    playerView = new SimpleExoPlayerView(this);
    playerView = (SimpleExoPlayerView) findViewById(R.id.exo_player_view);
    playerView.setControllerVisibilityListener(this);
    playerView.requestFocus();
    tracker = ((Tv2zApplication) getApplication()).getTracker();


private MediaDetails setMediaDetails() 
    medDetails = new MediaDetails();
    medDetails.setMediaId(CustPlugin.generateUniqueId());
    medDetails.setMediaTitle("Test Exo Android Player");
    medDetails.setMediaUrl(videoURL);
    medDetails.setPlayerName("Exo Android Player");
    medDetails.setWidth("1280");
    medDetails.setHeight("960");
    medDetails.setMediaLength((int)player.getDuration()/1000);
    medDetails.setCdUserId("EXO001");
    medDetails.setCdMediaId("1234");
    medDetails.setCdProgramType("VOD");
    medDetails.setCdProgramId("4567");
    medDetails.setCdDuration(52);
    return medDetails;

/**
 // time_to_initial_play will be ZERO, if "shouldAutoPlay" is true
 // The Function onStart() and onResume() are same, as triggered only once when player is loaded
 // Difference is that onStart is triggered for API 23 and up while onResume for API less than 23
 **/
@Override
protected void onStart() 
    super.onStart();
    if (Util.SDK_INT > 23) 
        initializePlayer();
    


@Override
protected void onResume() 
    super.onResume();
    hideSystemUi();
        if (Util.SDK_INT <= 23 || player == null) 
    initializePlayer();
       

@Override
protected void onStop() 
    super.onStop();
    releasePlayer();


@Override
public boolean dispatchKeyEvent(KeyEvent event) 

    // See whether the player view wants to handle media or DPAD keys events.

    return playerView.dispatchKeyEvent(event) || super.dispatchKeyEvent(event);




private void initializePlayer() 
    try 
        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
        player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
        Uri videoURI = Uri.parse(videoURL);
        DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer_video");
        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
        MediaSource mediaSource = new ExtractorMediaSource(videoURI, dataSourceFactory, extractorsFactory, null, null);
       //playerView.setPlayer(player);

        if (isTimelineStatic) 
            if (playerPosition == C.TIME_UNSET) 
                player.seekToDefaultPosition(playerWindow);
             else 
                player.seekTo(playerWindow, playerPosition);
            
        
        player.prepare(mediaSource);
        player.setPlayWhenReady(shouldAutoPlay);
        medDetails =  setMediaDetails();
        plugin = new CustPlugin(player,tracker,medDetails);
        plugin.startMonitoring();
        playerView.setPlayer(player);
    catch (Exception e)
        Toast.makeText(this, "Failed to Initialize!", Toast.LENGTH_LONG).show();
        Log.e("Demo Activity"," Exoplayer Error "+ e.toString());
    


@Override
public void onClick(View v) 
    switch (v.getId())
        case R.id.exo_play:
            Toast.makeText(this,"Video Resumed",Toast.LENGTH_LONG).show();
            break;
        case R.id.exo_ffwd:
            Toast.makeText(this,"FFWD clicked",Toast.LENGTH_LONG).show();
            break;
        case R.id.exo_prev:
            Toast.makeText(this,"REW clicked",Toast.LENGTH_LONG).show();
            break;
        default:
            break;
    


private void releasePlayer()
    if (player != null) 
        playbackPosition = player.getCurrentPosition();
        currentWindow = player.getCurrentWindowIndex();
        shouldAutoPlay = player.getPlayWhenReady();
        player.setVideoDebugListener(null);
        player.setAudioDebugListener(null);
        playerPosition = C.TIME_UNSET;
        Timeline timeline = player.getCurrentTimeline();
        if (!timeline.isEmpty() && timeline.getWindow(playerWindow, window).isSeekable) 
            playerPosition = player.getCurrentPosition();
        
        player.release();
        player = null;
        plugin.stopMonitoring();
    


@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 onVisibilityChange(int visibility) 



// End Class

【问题讨论】:

请提供第32行有&amp;#60;SimpleExoPlayerView&amp;#62;的布局文件。另外请告诉您的android版本和支持版本。 注意:我已经尝试在 XML 中使用 FrameLayout 和 Fragment,但仍然出现同样的问题。 我的 android Studio 版本是 3.0.1 在模拟器的 android API 25 上运行应用程序,我的手机是 redmi4 API 25,而且只有一个 xml 文件。 这些是依赖项: compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.jakewharton.timber:timber:4.5.1' compile 'com.android.support: support-v4:25.3.1' compile 'com.android.support:design:25.3.1' compile 'com.jakewharton:butterknife:8.4.0' apt 'com.jakewharton:butterknife-compiler:8.4.0' compile ' com.google.android.exoplayer:exoplayer:r2.5.2' 编译 'com.google.android.gms:play-services-location:9.8.0' 尝试在 attrsx.xml 中声明 SimpleExoPlayerView_shutter_background_color。这是您的 android(手机)无法找到的属性。 【参考方案1】:

似乎这是一个已知问题,docs 说你应该使用它(找到解决方案 here):

compileOptions 
    targetCompatibility JavaVersion.VERSION_1_8

我现在已经对其进行了测试,它似乎可以在最新版本的 ExoPlayer 上正常工作。

【讨论】:

你把这个放在哪个文件里? @victorfau 在 build.gradle 文件中。【参考方案2】:

尝试更改您的 XML 文件:

<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/exo_player_view"
android:focusable="true"
android:layout_
android:layout_
android:layout_below="@+id/resolution_textView"
android:layout_marginTop="2dp" />

到:

<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/exo_player_view"
android:focusable="true"
android:layout_
android:layout_
android:layout_below="@+id/resolution_textView"
android:layout_marginTop="2dp" />

然后在你的 DemoActivity 中改变:

private SimpleExoPlayerView playerView;

到:

private PlayerView playerView;

另外,请确保您的投射正确:

playerView = (PlayerView) findViewById(R.id.exo_player_view);

请注意,我使用的是 2.7.3 版本:

compile 'com.google.android.exoplayer:exoplayer:2.7.3'

【讨论】:

在更改为 playerView 后,崩溃得到修复,但发生了其他崩溃: E AndroidRuntime: Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatSeekBar cannot be cast to com.google.android.exoplayer2.ui.TimeBar 07-11 02:39:42.313 11914 11914 E AndroidRuntime:在 com.google.android.exoplayer2.ui.PlayerControlView.(PlayerControlView.java:311) 然后尝试用 TimeBar 替换 AppCompatSeekBar 我遇到了同样的问题,我使用 com.google.android.exoplayer2.ui.PlayerView 和最新版本的 exoplayer。当我将版本更改为 2.8.4 时,它运行良好。怎么会?见这里:github.com/google/ExoPlayer/issues/5138。【参考方案3】:

因为我有几个模块,所以我之前将依赖项添加到 .app 模块 build.gradle。我最终通过在相应模块的 build.gradle 中添加依赖项解决了这个问题。

implementation 'com.google.android.exoplayer:exoplayer-core:2.12.0'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.12.0'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.12.0'

【讨论】:

【参考方案4】:

将 compileOptions 粘贴到 android 范围内,如下所示:

   android 
        compileOptions
              sourceCompatibility JavaVersion.VERSION_1_8
              targetCompatibility JavaVersion.VERSION_1_8
       
    

【讨论】:

以上是关于膨胀类 com.google.android.exoplayer2.ui.SimpleExoPlayerView 时出错的主要内容,如果未能解决你的问题,请参考以下文章

膨胀类 CollapsingToolbarLayout 时出错

膨胀类片段时出错

使用上下文类膨胀布局

如何为android修复“错误膨胀类GeckoEngineView”

在 RecyclerView 上膨胀类 ImageView 时出错

膨胀类 com.google.android.material.slider.Slider 时出错