gpt4 book ai didi

java - 在 Android Studio 中运行 YouTube Activity 时出错

转载 作者:行者123 更新时间:2023-11-29 02:31:23 25 4
gpt4 key购买 nike

在 Android Studio 中开发 Youtube 应用。我不断收到错误消息:

The activity must be exported or contain an intent-filter

我已经阅读了文档并查看了在线内容,但我似乎无法找到解决方案。我该怎么办?我将在下面粘贴我的 main-activity 代码。

YoutubeActivity.java

package com.code.mohamedali.hackathon2018;

import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
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.YouTubePlayerView;

public class YoutubeActivity extends YouTubeBaseActivity
implements YouTubePlayer.OnInitializedListener {

private static final String TAG = "YoutubeActivity";
static final String GOOGLE_API_KEY = "AIzaSyDMS4J2hlSJqCZsNL1Owqcups91vGQiF3I";
static final String YOUTUBE_VIDEO = "d-diB65scQU";
static final String YOUTUBE_PLAYLIST = "RDQMhH0aPyrY-R0";

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

ConstraintLayout mylayout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_youtube,null);
setContentView(mylayout);

YouTubePlayerView playerView = new YouTubePlayerView(this);
playerView.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mylayout.addView(playerView);
playerView.initialize(GOOGLE_API_KEY,this);
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {

Log.d(TAG, "onInitializationSuccess: provider is " + provider.getClass().toString() );
Toast.makeText(this,"Initliazed youtube player succesfully" , Toast.LENGTH_LONG).show();


// Now we are using the Event Listener and the StateChange Listener
youTubePlayer.setPlaybackEventListener(playbackEventListener);
youTubePlayer.setPlayerStateChangeListener(playerStateChangeListener);



if(!wasRestored){
youTubePlayer.cueVideo(YOUTUBE_VIDEO);
}


}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

final int REQUEST_CODE = 1;

if(youTubeInitializationResult.isUserRecoverableError()){
youTubeInitializationResult.getErrorDialog(this,REQUEST_CODE).show();
}else{
String errorMessage = String.format("There was an error initialiazing youtube player (%1$s)", youTubeInitializationResult.toString());
Toast.makeText(this,errorMessage,Toast.LENGTH_LONG).show();
}


}


private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() {
@Override
public void onPlaying() {
Toast.makeText(YoutubeActivity.this, "Good, video is playing ok", Toast.LENGTH_LONG).show();

}

@Override
public void onPaused() {
Toast.makeText(YoutubeActivity.this, "Good, video is paused", Toast.LENGTH_LONG).show();
}

@Override
public void onStopped() {
Toast.makeText(YoutubeActivity.this,"Video has been stopped" , Toast.LENGTH_LONG).show();


}

@Override
public void onBuffering(boolean b) {

}

@Override
public void onSeekTo(int i) {

}
};

private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
@Override
public void onLoading() {

}

@Override
public void onLoaded(String s) {

}

@Override
public void onAdStarted() {
Toast.makeText(YoutubeActivity.this,"Click Ad now, make me rich" , Toast.LENGTH_LONG).show();


}

@Override
public void onVideoStarted() {
Toast.makeText(YoutubeActivity.this,"Video has started" , Toast.LENGTH_LONG).show();


}

@Override
public void onVideoEnded() {
Toast.makeText(YoutubeActivity.this,"You have reached the end of the
video" , Toast.LENGTH_LONG).show();


}

@Override
public void onError(YouTubePlayer.ErrorReason errorReason) {

}
};

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.code.mohamedali.hackathon2018">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".YoutubeActivity"
android:label="Youtube Player">
</activity>

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />

<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">

</activity>
</application>

</manifest>

最佳答案

尝试将 android:exported="true" 添加到 Activity

<activity android:name=".YoutubeActivity"
android:exported="true"
android:label="Youtube Player">
</activity>

确保应用程序的运行配置包含 MainActivity 作为启动器。

关于java - 在 Android Studio 中运行 YouTube Activity 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49462029/

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