gpt4 book ai didi

android - 不幸的是,youtube 播放器已停止工作

转载 作者:行者123 更新时间:2023-12-02 12:52:18 26 4
gpt4 key购买 nike

我已将 api jar 文件放在 libs 文件夹中。当我运行此程序时,我得到不幸的是,youtube 播放器已停止工作。

有人可以帮助我吗?

我还想知道一件事:我在java文件中使用了下面的语句。它在DEVELOPER_KEY处显示红色标记(实际上这是一个存储api key 的变量), youTubeView.initialize(Config.DEVELOPER_KEY, this);这就是为什么我使用以下声明:youTubeView.initialize(DEVELOPER_KEY,this);

因此有什么问题吗

这是我的activity.java

    package com.example.XXX.XXX;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

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.PlayerStyle;
import com.google.android.youtube.player.YouTubePlayerView;

public class YoutubeActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
public static final String DEVELOPER_KEY = "<Censored>";
public static final String YOUTUBE_VIDEO_CODE = "uOpVvokKnu0";

private static final int RECOVERY_DIALOG_REQUEST = 1;

// YouTube player view
private YouTubePlayerView youTubeView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.activity_main);

youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);

// Initializing video player with developer key
youTubeView.initialize(DEVELOPER_KEY,this);

}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
getString(R.string.error_player), errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {

// loadVideo() will auto play video
// Use cueVideo() method, if you don't want to play it automatically
player.loadVideo(YOUTUBE_VIDEO_CODE);

// Hiding player controls
player.setPlayerStyle(PlayerStyle.CHROMELESS);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(DEVELOPER_KEY,this);
}
}

private YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}

}

`

这是我的 XML 代码

`

        <ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="@drawable/snake_bg" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:orientation="vertical" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/rouned_corner_shadow"
android:gravity="center_horizontal"
android:orientation="vertical" >

<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_marginBottom="20dp"
android:scaleType="fitCenter"
android:src="@drawable/nat_geo_logo" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/title_logo"
android:textColor="@color/title"
android:textSize="20dp"
android:textStyle="bold" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginBottom="30dp"
android:scaleType="fitCenter"
android:src="@drawable/wild" />
</LinearLayout>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:background="@drawable/rouned_corner_shadow"
android:text="@string/btn_skip_intro" />
</LinearLayout>

</RelativeLayout>

`

记录猫输出:

at android.app.ActivityThread.access$800(ActivityThread.java:135) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.health.weshallovercome.YoutubeActivity.onCreate(YoutubeActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

由于下面的代码,我在设计 View 上遇到了一些错误。我会附上它。

<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp" />

That is error I'm getting on XML file

this is the continuation for this error

请找到 logcat:

05-04 06:24:29.670      935-935/XXX D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
05-04 06:24:31.580 935-935/XXX D/dalvikvm﹕ GC_FOR_ALLOC freed 48K, 5% free 2808K/2940K, paused 248ms, total 333ms
05-04 06:24:31.580 935-935/XXX I/dalvikvm-heap﹕ Grow heap (frag case) to 9.002MB for 6480016-byte allocation
05-04 06:24:32.030 935-944/XXX D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 2% free 9136K/9272K, paused 453ms, total 453ms
05-04 06:24:33.410 935-935/XXX D/﹕ HostConnection::get() New Host Connection established 0xb7550c78, tid 935
05-04 06:24:33.550 935-935/XXX W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-04 06:24:33.570 935-935/XXX D/OpenGLRenderer﹕ Enabling debug mode 0
05-04 06:28:24.979 935-935/XXX W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-04 06:28:27.559 935-935/XXX D/dalvikvm﹕ GC_FOR_ALLOC freed 29K, 2% free 11093K/11216K, paused 41ms, total 42ms
05-04 06:28:27.769 935-935/XXX W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-04 06:28:30.839 935-935/XXX D/AndroidRuntime﹕ Shutting down VM
05-04 06:28:30.839 935-935/XXX W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3ab8ba8)
05-04 06:28:30.869 935-935/XXX E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: XXX, PID: 935
java.lang.RuntimeException: Unable to start activity ComponentInfo{XXX/XXX.YoutubeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at XXX.YoutubeActivity.onCreate(YoutubeActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
05-04 06:28:35.489 935-935/XXX I/Process﹕ Sending signal. PID: 935 SIG: 9
05-04 06:28:36.699 1148-1148/XXX D/dalvikvm﹕ GC_FOR_ALLOC freed 40K, 5% free 2809K/2936K, paused 39ms, total 40ms
05-04 06:28:36.939 1148-1161/XXX D/dalvikvm﹕ GC_FOR_ALLOC freed 16K, 3% free 3308K/3408K, paused 24ms, total 24ms
05-04 06:28:37.219 1148-1148/XXX D/﹕ HostConnection::get() New Host Connection established 0xb769ae50, tid 1148
05-04 06:28:37.289 1148-1148/XXX W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-04 06:28:37.309 1148-1148/XXX D/OpenGLRenderer﹕ Enabling debug mode 0

我已按照以下链接在 Android 应用程序中播放 YouTube。 http://www.androidhive.info/2014/12/how-to-play-youtube-video-in-android-app/我在应用程序中所做的更改是:我在 Androidmanifest.xml 下添加了这一行,如前所述:我在 build.gradle 中添加了以下行:编译文件('libs/YouTubeAndroidPlayerApi.jar')

我已将此 jar 添加到 libs 文件夹中。我已经在这个问题中保留了我的xml和java文件的代码。我完全按照该教程进行操作。

由于 xml 中的以下代码,您没有遇到任何问题吗:

<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp" />

我在java文件夹中创建了config.java:

config.java的代码为:

public class config {
// Google Console APIs developer key
// Replace this key with your's
public static final String DEVELOPER_KEY = "";

// YouTube video id
public static final String YOUTUBE_VIDEO_CODE = "uOpVvokKnu0"; }

Screenshot of where I have added jar file

由于我没有 20 个声望,所以无法聊天。我还有一个帐户,它有超过 20 个声誉。但我在那里找不到你。

我们快到了,请帮助我。

现在可以启用聊天功能吗?我有超过20个声誉。我可以聊天。

还请找到当前的 logcat:

05-06 02:27:38.110 991-991/com.example.health.weshallovercome D/AndroidRuntime﹕ Shutting down VM
05-06 02:27:38.110 991-991/com.example.health.weshallovercome W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3a3fba8)
05-06 02:27:38.140 991-991/com.example.health.weshallovercome E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.health.weshallovercome, PID: 991
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.health.weshallovercome/com.example.health.weshallovercome.YoutubeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.health.weshallovercome.YoutubeActivity.onCreate(YoutubeActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
05-06 02:27:46.480 991-991/com.example.health.weshallovercome I/Process﹕ Sending signal. PID: 991 SIG: 9
05-06 02:27:47.510 1010-1010/com.example.health.weshallovercome D/dalvikvm﹕ GC_FOR_ALLOC freed 59K, 5% free 2809K/2940K, paused 52ms, total 53ms
05-06 02:27:47.510 1010-1010/com.example.health.weshallovercome I/dalvikvm-heap﹕ Grow heap (frag case) to 3.258MB for 472516-byte allocation
05-06 02:27:47.560 1010-1019/com.example.health.weshallovercome D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 4% free 3271K/3404K, paused 39ms, total 39ms
05-06 02:27:47.960 1010-1010/com.example.health.weshallovercome D/﹕ HostConnection::get() New Host Connection established 0xb7c64710, tid 1010
05-06 02:27:48.050 1010-1010/com.example.health.weshallovercome W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-06 02:27:48.070 1010-1010/com.example.health.weshallovercome D/OpenGLRenderer﹕ Enabling debug mode 0
05-06 02:27:50.180 1010-1010/com.example.health.weshallovercome D/dalvikvm﹕ GC_FOR_ALLOC freed 24K, 3% free 3597K/3692K, paused 30ms, total 31ms
05-06 02:27:50.280 1010-1010/com.example.health.weshallovercome D/dalvikvm﹕ GC_FOR_ALLOC freed 3K, 3% free 3966K/4068K, paused 30ms, total 31ms
05-06 02:27:50.380 1010-1010/com.example.health.weshallovercome D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 3% free 4338K/4444K, paused 31ms, total 31ms
05-06 02:27:50.760 1010-1010/com.example.health.weshallovercome W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-06 02:27:54.760 1010-1010/com.example.health.weshallovercome D/dalvikvm﹕ GC_FOR_ALLOC freed 22K, 2% free 4837K/4928K, paused 34ms, total 34ms
05-06 02:27:54.970 1010-1010/com.example.health.weshallovercome I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
05-06 02:27:54.970 1010-1010/com.example.health.weshallovercome W/dalvikvm﹕ VFY: unable to resolve virtual method 12609: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V

正如您所说,我在右键菜单中没有找到“添加为库”选项。而且我也不是通过工作室添加了 jar 文件。我已经打开了 Androidstudioprojects(存在于 C 中),并且选择了我的项目,因为我打开了库并复制了 jar 文件。它显示了我项目中 libs 下的 jar 文件。我不认为这是添加 jar 的错误方法,是吗? right click menu

最佳答案

我已经测试了你的代码,当问题是互联网许可时,我感觉自己像个傻瓜。将其添加到您的 AndroidManifest.xml 中:

<uses-permission  android:name="android.permission.INTERNET"/>

根据 YoutubePlayer API 文档所述,确保您拥有有效的 API key 。

If you do not set the developer key, the samples will throw a java.lang.NullPointerException and close when you attempt to run them.

编辑:

要修复布局渲染问题,请使用tools:context as

<AnyRootLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".YouTubeActivit" <!-- Your activity name here -->
>
<!-- Your child elements here -->
</AnyRootElement>

将 jar 文件添加到 libs 文件夹中,然后右键单击该 jar 文件,然后单击“添加为库”有关在 android studio 中添加 jar 文件的更多信息 here

我的 Build.gradle(用于应用程序模块):

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "21.1.2"

defaultConfig {
applicationId "com.mypackage.youtubedemo"
minSdkVersion 9
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile files('libs/YouTubeAndroidPlayerApi.jar')
}

关于android - 不幸的是,youtube 播放器已停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30001150/

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