我正在尝试将DrawerLayout与YouTubePlayerSupportFragment集成到我的应用程序中。我想使用导航抽屉菜单在主要 Activity 中加载片段。
我已经下载了“YouTubeAndroidPlayerApi.jar”文件,并将其放入“libs”文件夹中。在com.android.support中,我还在build.gradle(app)文件中添加了用于appcompact,设计和支持的依赖项:
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.test.xyz.testing"
minSdkVersion 19
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile files('libs/YouTubeAndroidPlayerApi.jar')
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:support-v4:24.2.1'
testCompile 'junit:junit:4.12'
}
这是“activity_main.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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/colorPrimary"
tools:context="com.test.xyz.testing.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="#ffffff"
android:layout_centerInParent="true"
android:text="Relative Layout" />
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_alignParentTop="true"
android:background="#333333"
></FrameLayout>
</RelativeLayout>
<ListView
android:id="@+id/listView"
android:layout_width="500dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@android:color/holo_blue_dark"
>
</ListView>
</android.support.v4.widget.DrawerLayout>
我添加了两个片段:一个简单的片段,另一个用于YouTubePlayerSupportFragment。
这是YoutubePlayerSupportFragment的代码(这会导致应用崩溃):
package com.test.xyz.testing;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.google.android.youtube.player.YouTubeBaseActivity;
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.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerFragment;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
import com.google.android.youtube.player.YouTubePlayerView;
public class PlayerYoutubeFrag extends YouTubePlayerSupportFragment {
private String currentVideoID = "video_id";
private YouTubePlayer activePlayer;
public static PlayerYoutubeFrag newInstance(String url) {
PlayerYoutubeFrag playerYouTubeFrag = new PlayerYoutubeFrag();
Bundle bundle = new Bundle();
bundle.putString("url", url);
playerYouTubeFrag.setArguments(bundle);
playerYouTubeFrag.init(); //This line right here
return playerYouTubeFrag;
}
private void init() {
initialize(Config.YOUTUBE_ANDROID_KEY, new OnInitializedListener() {
@Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) { }
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
activePlayer = player;
activePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
if (!wasRestored) {
activePlayer.loadVideo(getArguments().getString("url"), 0);
}
}
});
}
}
这是“MainActivity.java”文件的代码:
package com.test.xyz.testing;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import layout.SettingsFragment;
public class MainActivity extends AppCompatActivity
implements SettingsFragment.OnFragmentInteractionListener {
ListView listView;
ArrayAdapter<String> listAdapter;
String fragmentArray[] = {"Frag1", "Frag2"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
listAdapter = new ArrayAdapter<String>(this, R.layout.list_text, R.id.list_text_view, fragmentArray);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Fragment fragment;
switch (i) {
case 0:
fragment = new SettingsFragment();
break;
case 1:
fragment = null;
break;
default:
fragment = null;
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
if (fragment != null) {
fragmentManager.beginTransaction().replace(R.id.frame, fragment).commit();
}
else {
String video_id = "LHcP4MWABGY";
PlayerYoutubeFrag myFragment = PlayerYoutubeFrag.newInstance(video_id);
fragmentManager.beginTransaction().replace(R.id.frame, myFragment).commit();
}
}
});
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
导航抽屉工作正常。基本上,如果我单击抽屉的第一个条目,则会正确加载SettingsFragment。如果我单击Drawer的第二个条目,则应加载PlayerYoutubeFrag(扩展了YouTubePlayerSupportFragment),并传递一个字符串video_id。
应用程序在最后一行崩溃:
String video_id = "LHcP4MWABGY";
PlayerYoutubeFrag myFragment = PlayerYoutubeFrag.newInstance(video_id);
fragmentManager.beginTransaction().replace(R.id.frame, myFragment).commit();
这是日志:
FATAL EXCEPTION: main
Process: com.test.xyz.testing, PID: 28303
java.lang.SecurityException: Not allowed to bind to service Intent { act=com.google.android.youtube.api.service.START pkg=com.google.android.youtube }
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1460)
at android.app.ContextImpl.bindService(ContextImpl.java:1418)
at android.content.ContextWrapper.bindService(ContextWrapper.java:632)
at com.google.android.youtube.player.internal.r.e(Unknown Source)
at com.google.android.youtube.player.YouTubePlayerView.a(Unknown Source)
at com.google.android.youtube.player.YouTubePlayerSupportFragment.a(Unknown Source)
at com.google.android.youtube.player.YouTubePlayerSupportFragment.onCreateView(Unknown Source)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
不确定如何解决此问题。还是有人可以使用抽屉和youtube片段向我指出一个应用的工作示例?
最佳答案
您是否允许 list 文件中的Internet访问
uses-permission android:name="android.permission.INTERNET"
设备上有youtube应用吗?