gpt4 book ai didi

Android:使用 DrawerLayout 和 YouTubePlayerSupportFragment

转载 作者:行者123 更新时间:2023-12-03 06:18:36 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序中集成 DrawerLayoutYouTubePlayerSupportFragment .我想使用抽屉导航菜单在主要 Activity 中加载 fragment 。

我已下载“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>

我添加了两个 fragment :一个简单 fragment 和一个用于 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) {

}
}

抽屉导航工作正常。基本上,如果我单击 Drawer 的第一个条目,则 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 fragment 的应用程序的工作示例吗?

最佳答案

您是否允许在 list 文件中访问互联网

uses-permission android:name="android.permission.INTERNET"

设备上是否有 youtube 应用程序?

关于Android:使用 DrawerLayout 和 YouTubePlayerSupportFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39979971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com