YouTubePlayer 与 AppCompat v7 库
YouTubePlayer together with AppCompat v7 library
我开发了一个应用程序,它使用 YouTube android API
在应用程序内播放某个视频。当时我使用了目标 API 19,也使用了目标 API 19。现在我想使用目标 API 22 将应用切换到 Material Design
的设计更改并且还使用 AppCompat-v7 :22+ library
。但是 YouTubeBaseActivity
打不开,应用程序崩溃了。下面我将post我到目前为止所做的。
EventDetail.java
public class EventDetail extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener { .
.
.
protected void onCreate(Bundle savedInstanceState) {
// remove the shadow from action bar
setTheme(R.style.Theme_TranslucentActionBar_ActionBar_NoOverlay);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_detail);
// enable back button
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// setting the action bar initially transparent
mActionBarBackgroundDrawable = new ColorDrawable(
Color.parseColor("#ec5c59"));
mActionBarBackgroundDrawable.setAlpha(0);
getSupportActionBar().setBackgroundDrawable(mActionBarBackgroundDrawable);
// binding the scrollview
...
清单文件
<activity
android:name="com.dev.apk.evente.al.EventDetail"
android:icon="@drawable/ikona_projekte"
android:label="@string/title_activity_event_detail"
android:screenOrientation="portrait"
android:theme="@style/Theme.TranslucentActionBar.ActionBar.Overlay" />
布局文件:
<RelativeLayout
android:id="@+id/video_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:paddingLeft="23dp"
android:paddingRight="23dp"
android:paddingBottom="15dp"
android:background="@drawable/event_background_block" >
<TextView
android:id="@+id/video_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/white"
android:gravity="left"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="Video"
android:textColor="@color/VeryDarkGray"
android:textSize="22sp" >
</TextView>
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/video_label"
android:background="@color/white" />
</RelativeLayout>
Theme.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.TranslucentActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/Widget.ActionBar</item>
</style>
<style name="Theme.TranslucentActionBar.ActionBar" />
<style name="Theme.TranslucentActionBar.ActionBar.Overlay">
<item name="android:actionBarStyle">@style/Widget.ActionBar.Transparent</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="colorPrimary">@color/evente_date_color</item>
<item name="colorPrimaryDark">@color/evente_darken_color</item>
</style>
<!-- Theme without ActionBar shadow (inherits main theme) -->
<style name="Theme.TranslucentActionBar.ActionBar.NoOverlay" parent="Theme.TranslucentActionBar.ActionBar.Overlay">
<item name="android:windowContentOverlay">@null</item>
</style>
</resources>
所以一开始打不开,因为方法getActionBar()
return 为空,后来我改成getSupportActionBar()
但是不认识。然后最后我在下面更改了这一行:
public class EventDetail extends AppCompatActivity implements
YouTubePlayer.OnInitializedListener {
它给了我以下错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myikub.al/com.dev.apk.evente.al.EventDetail}: android.view.InflateException: Binary XML file line #250: Error inflating class com.google.android.youtube.player.YouTubePlayerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access0(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #250: Error inflating class com.google.android.youtube.player.YouTubePlayerView
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.dev.apk.evente.al.EventDetail.onCreate(EventDetail.java:112)
at android.app.Activity.performCreate(Activity.java:5303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access0(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.dev.apk.evente.al.EventDetail.onCreate(EventDetail.java:112)
at android.app.Activity.performCreate(Activity.java:5303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access0(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context.
at com.google.android.youtube.player.YouTubePlayerView.<init>(Unknown Source)
....
我正在使用 Android Studio。任何想法将不胜感激。
根据堆栈跟踪,activity 在使用 YouTube API
时必须扩展 YouTubeBaseActivity
如果您需要一个操作栏,那么我认为最好的方法是将样式更改为没有操作栏,并在您的视图中使用 toolbar。
问题是您使用的是 YouTubePlayerView
, which requires your Activity
to extend YouTubeBaseActivity
。然后你改变了你的activity,现在它扩展了AppCompatActivity
。这打破了 YouTubePlayerView
.
解决方案非常简单:使用 YouTubePlayerFragment
class。这对 Activity
没有任何要求,让您有很多主题选择。
将此代码粘贴到 gradle 应用程序中
compile 'com.google.android.gms:play-services:8.4.0'
compile files('libs/YouTubeAndroidPlayerApi.jar')
我开发了一个应用程序,它使用 YouTube android API
在应用程序内播放某个视频。当时我使用了目标 API 19,也使用了目标 API 19。现在我想使用目标 API 22 将应用切换到 Material Design
的设计更改并且还使用 AppCompat-v7 :22+ library
。但是 YouTubeBaseActivity
打不开,应用程序崩溃了。下面我将post我到目前为止所做的。
EventDetail.java
public class EventDetail extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener { .
.
.
protected void onCreate(Bundle savedInstanceState) {
// remove the shadow from action bar
setTheme(R.style.Theme_TranslucentActionBar_ActionBar_NoOverlay);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_detail);
// enable back button
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// setting the action bar initially transparent
mActionBarBackgroundDrawable = new ColorDrawable(
Color.parseColor("#ec5c59"));
mActionBarBackgroundDrawable.setAlpha(0);
getSupportActionBar().setBackgroundDrawable(mActionBarBackgroundDrawable);
// binding the scrollview
...
清单文件
<activity
android:name="com.dev.apk.evente.al.EventDetail"
android:icon="@drawable/ikona_projekte"
android:label="@string/title_activity_event_detail"
android:screenOrientation="portrait"
android:theme="@style/Theme.TranslucentActionBar.ActionBar.Overlay" />
布局文件:
<RelativeLayout
android:id="@+id/video_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:paddingLeft="23dp"
android:paddingRight="23dp"
android:paddingBottom="15dp"
android:background="@drawable/event_background_block" >
<TextView
android:id="@+id/video_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/white"
android:gravity="left"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="Video"
android:textColor="@color/VeryDarkGray"
android:textSize="22sp" >
</TextView>
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/video_label"
android:background="@color/white" />
</RelativeLayout>
Theme.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.TranslucentActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/Widget.ActionBar</item>
</style>
<style name="Theme.TranslucentActionBar.ActionBar" />
<style name="Theme.TranslucentActionBar.ActionBar.Overlay">
<item name="android:actionBarStyle">@style/Widget.ActionBar.Transparent</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="colorPrimary">@color/evente_date_color</item>
<item name="colorPrimaryDark">@color/evente_darken_color</item>
</style>
<!-- Theme without ActionBar shadow (inherits main theme) -->
<style name="Theme.TranslucentActionBar.ActionBar.NoOverlay" parent="Theme.TranslucentActionBar.ActionBar.Overlay">
<item name="android:windowContentOverlay">@null</item>
</style>
</resources>
所以一开始打不开,因为方法getActionBar()
return 为空,后来我改成getSupportActionBar()
但是不认识。然后最后我在下面更改了这一行:
public class EventDetail extends AppCompatActivity implements
YouTubePlayer.OnInitializedListener {
它给了我以下错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myikub.al/com.dev.apk.evente.al.EventDetail}: android.view.InflateException: Binary XML file line #250: Error inflating class com.google.android.youtube.player.YouTubePlayerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access0(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #250: Error inflating class com.google.android.youtube.player.YouTubePlayerView
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.dev.apk.evente.al.EventDetail.onCreate(EventDetail.java:112)
at android.app.Activity.performCreate(Activity.java:5303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access0(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.dev.apk.evente.al.EventDetail.onCreate(EventDetail.java:112)
at android.app.Activity.performCreate(Activity.java:5303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access0(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context.
at com.google.android.youtube.player.YouTubePlayerView.<init>(Unknown Source)
....
我正在使用 Android Studio。任何想法将不胜感激。
根据堆栈跟踪,activity 在使用 YouTube API
时必须扩展 YouTubeBaseActivity如果您需要一个操作栏,那么我认为最好的方法是将样式更改为没有操作栏,并在您的视图中使用 toolbar。
问题是您使用的是 YouTubePlayerView
, which requires your Activity
to extend YouTubeBaseActivity
。然后你改变了你的activity,现在它扩展了AppCompatActivity
。这打破了 YouTubePlayerView
.
解决方案非常简单:使用 YouTubePlayerFragment
class。这对 Activity
没有任何要求,让您有很多主题选择。
将此代码粘贴到 gradle 应用程序中
compile 'com.google.android.gms:play-services:8.4.0'
compile files('libs/YouTubeAndroidPlayerApi.jar')