Error inflating class com.google.android.exoplayer2.ui.SimpleExoPlayerView

▼魔方 西西 提交于 2019-12-22 18:23:17

问题


One Strange thing: the code is working fine while using emulator, but crashes while running building an unsigned apk.

when connected to android studio, it gives the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.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)

This is my xml file:

<?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_width="match_parent"
android:layout_height="match_parent"
tools:context=".DemoActivity">

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

<TextView
android:id="@+id/resolution_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/resolution_textView"
android:layout_marginTop="2dp" />
</RelativeLayout>

This is my DemoActivity file: public class DemoActivity extends AppCompatActivity implements 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

回答1:


Seems it's a known issue, which the docs say you should use this (found solution here) :

compileOptions {
    targetCompatibility JavaVersion.VERSION_1_8
}

I've tested it now, and it seems to work fine with newest version of ExoPlayer.




回答2:


Try changing your XML file from:

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

to:

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

Then in your DemoActivity change:

private SimpleExoPlayerView playerView;

to:

private PlayerView playerView;

Also, make sure that your casting is correct:

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

Note that I'm using version 2.7.3:

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


来源:https://stackoverflow.com/questions/49533046/error-inflating-class-com-google-android-exoplayer2-ui-simpleexoplayerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!