继承ActionBarSherlock和Android YouTubePlayerActionBarSherlock、Android、YouTubePlayer

2023-09-05 01:20:15 作者:清闲人

我已经开发了一个应用程序,用于播放YouTube视频使用ActionBarSherlock。

I've developed an app for playing youtube videos using ActionBarSherlock.

现在的YouTubePlayer API为Android可用(此处),我想融入我的应用程序这对提高播放和控制。

Now that the YouTubePlayer api for android is available (here), I want to integrate this into my app to improve playback and controls.

我碰到的一个问题,因为我需要使用多重继承我的活动都扩展 SherlockActivity YouTubeBaseActivity

I've run into an issue, in that I need to use multiple inheritance for my activity to both extend SherlockActivity and also YouTubeBaseActivity.

我检查了这篇文章试图了解多重继承的Java ,但坦率地说这是在我的头上。

I checked out this article to try to understand multiple inheritance in Java, but frankly it's over my head.

如果我试图做这样这我得到的,我不能实例化问题 SherlockActivity

If I attempt to do something like this I get the issue that I can't instantiate SherlockActivity.

任何人有一些具体的例子如何扩展两个类?有没有人有扩大二者 SherlockActivity 和一些其他类,你是怎么做到?

Anyone have some concrete example of how to extend both classes? Has anyone had to extend both SherlockActivity and some other class, and how did you accomplish?

推荐答案

我有同样的问题 - 我想YouTube播放器添加到我的应用程序,但ALBO我并不想从它删除夏洛特(基于支持库)。什么是坏,我不是能够使用任何playbers的,因为我得到了错误(充气段,YouTubePlayerView不能启动,无需特别活动等)。

I had the same problems - I wanted to add YouTube player to my app, but albo I don't wanted to delete Sherlock from it (based on support library). And what is bad, I wasnt able to use any of the playbers, because I got errors (inflating fragment, YouTubePlayerView cant start without special Activity and so on).

什么工作:我用SherlockFragmentActivity,FragmentManager(getSupportFragmentManager())和YouTubePlayerSupportFragment。相反,将其添加到XML中,我创建了从code。我的布置是这样的:

What worked: I used SherlockFragmentActivity, FragmentManager (getSupportFragmentManager()) and YouTubePlayerSupportFragment. Instead of adding it to XML, I created everything from code. My layout looks like this:

<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=".MainActivity" >

<LinearLayout
    android:id="@+id/fragmentz"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
</LinearLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

和Java code:

and Java code:

package com.example.youtubetesting;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.OnInitializedListener;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends SherlockFragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();

    YouTubePlayerSupportFragment fragment = new YouTubePlayerSupportFragment();
    fragmentTransaction.add(R.id.fragmentz, fragment);
    fragmentTransaction.commit();

    fragment.initialize("Your API KEY HERE",
            new OnInitializedListener() {

                @Override
                public void onInitializationSuccess(Provider arg0,
                        YouTubePlayer arg1, boolean arg2) {
                    if (!arg2) {
                        arg1.loadVideo("wKJ9KzGQq0w");
                    }
                }

                @Override
                public void onInitializationFailure(Provider arg0,
                        YouTubeInitializationResult arg1) {
                }

            });
}

}

我不知道为什么Android的返航错误,当我在不断膨胀的观点在正常的方式,但这种完美的作品。

I dont know why Android was returning errors when I was inflating views in normal way, but this works perfectly.