找到你要的答案

Q:Unfortunately,youtube player has stopped working

Q:不幸的是,YouTube播放器已停止工作

I have placed api jar file in libs folder.When I run this program,I'm getting Unfortunately,youtube player has stopped working.

Can anyone help me?

one more thing I want to know: I have used this below statement in java file. it is showing redmark at DEVELOPER_KEY(actually this is a variable storing api key), youTubeView.initialize(Config.DEVELOPER_KEY, this); That is why i have used below statement: youTubeView.initialize(DEVELOPER_KEY,this);

Is there any problem because of this

This is my 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);
        }

    }

`

This is my XML code

`

        <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>

`

Log cat output:

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)

Because of this below code I'm getting some errors on design view. I'll attach that.

<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" />

Please find the 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

I have followed this below link for playing youtube in android app. http://www.androidhive.info/2014/12/how-to-play-youtube-video-in-android-app/ Changes I have made in my app are: I added this below line Androidmanifest.xml as said earlier: I have added below line in build.gradle: compile files('libs/YouTubeAndroidPlayerApi.jar')

I have added this jar in libs folder. I have already keep the code of code my xml and java file in this question. I have followed that tutorial completely.

Are you not getting any problems because of below code in 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" />

And I have created config.java in java folder:

code of config.java as:

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";  }

As I don't have 20 reputations,I can't chat. I have one more account,it has more than 20 reputations.But i'm unable to find you there.

We are almost reached,Please help me.

Can you please enable the chat now? I have more than 20 reputations. I can chat.

And also please find the present 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

And as you told I didn't find add as library option in right click menu. And also I added the jar file not through studio. I have opened Androidstudioprojects(which is present in C) and I have choosen my project,in that I opened libs and copied jar file. It is showing that jar file under libs in my project.I dont think that is the wrong way of adding jar,is it? right click menu

I have placed api jar file in libs folder.When I run this program,I'm getting 不幸的是,YouTube播放器已停止工作.

有人能帮我吗?

one more thing I want to know: I have used this below statement in java file. it is showing redmark at DEVELOPER_KEY(actually this is a variable storing api key), youTubeView.initialize(Config.DEVELOPER_KEY, this); That is why i have used below statement: 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)

因为下面的代码,我在设计视图上有一些错误。我会附上。

<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" />

请看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

I have followed this below link for playing youtube in android app. http://www.androidhive.info/2014/12/how-to-play-youtube-video-in-android-app/ Changes I have made in my app are: I added this below line Androidmanifest.xml as said earlier: I have added below line in build.gradle: compile files('libs/YouTubeAndroidPlayerApi.jar')

I have added this jar in libs folder. I have already keep the code of code my xml and java file in this question. I have followed that tutorial completely.

你没有得到任何问题,因为下面的代码在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";  }

因为我没有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

And as you told I didn't find add as library option in right click menu. And also I added the jar file not through studio. I have opened Androidstudioprojects(which is present in C) and I have choosen my project,in that I opened libs and copied jar file. It is showing that jar file under libs in my project.I dont think that is the wrong way of adding jar,is it? right click menu

answer1: 回答1:

I have tested your code and i felt like fool when the issue is internet permission. Add this to your AndroidManifest.xml:

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

Make sure you have a valid API key since the YoutubePlayer API doc says.

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

EDIT:

For fixing the layout rendering issues use 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>

Add the jar file into the libs folder and right click on that jar file and click on "Add as library" more info on adding a jar file in android studio here

My Build.gradle (for app module):

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')
}

I have tested your code and i felt like fool when the issue is internet permission. Add this to your AndroidManifest.xml:

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

确保你有一个有效的API密钥,因为医生说youtubeplayer API。

如果你不设置开发的关键,样品将java.lang.nullpointerexception和关闭当您尝试运行。

编辑:

用于固定布局渲染问题使用工具:上下文为

<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文件到JAR文件并单击libs文件夹,右键单击“添加库”中加入Android Studio JAR文件,这里更多的信息

我的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  android-youtube-api