问题
For some reason, AppCompatActivity refuses to let me embed Youtube videos even though I am using the SupportFragment activity. The app crashes every time an activity contains a Youtube fragment. How can I fix this?
public class Insane1x1 extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {
public static final String API_KEY = "AIzaSyBqaaaaaaa";
public static final String VIDEO_ID = "3MvnRsItEmg"; //1-1
//private YouTubePlayer youTubePlayer;
private YouTubePlayerSupportFragment youTubePlayerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insane1x1);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
youTubePlayerFragment = (YouTubePlayerSupportFragment) getSupportFragmentManager()
.findFragmentById(R.id.youtubeplayerfragment);
youTubePlayerFragment.initialize(API_KEY, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.insane1x1, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
Toast.makeText(getApplicationContext(),
"YouTubePlayer.onInitializationFailure()",
Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
//youTubePlayer = player;
Toast.makeText(getApplicationContext(),
"YouTubePlayer.onInitializationSuccess()",
Toast.LENGTH_LONG).show();
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
And here's the xml file:
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youtubeplayerfragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Is it the YouTubePlayer that is crashing the app, when it is cuing the video in the onInitializationSuccess method??
回答1:
I think it's cz its not extending "YouTubeBaseActivity", for some reason though YouTubeBaseActivity doesn't have as much functionality as AppCompatActivity. Maybe there are newer versions. But I'dd say thats the problem.
来源:https://stackoverflow.com/questions/37098897/appcompatactivity-crashing-with-embedded-youtube-videos