加载youtube视频时出错:未知的youtube活动



我的代码中使用onClick Listener的部分没有resoncondition,也没有播放任何视频。


public class MainActivity extends AppCompatActivity {
private AdView mAdView;
private AdView mAdView2;
private static final String TAG = MainActivity.class.getSimpleName();
private RecyclerView firstrecyclerView;
ArrayList<VideoDetails> videoDetailsoArrayList;
String API_Key = "myKey`";
String url = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCVMWWQ985A_-SESZUy_SsVQ&maxResults=50&key=myKey";
adapter adapter;
VideoDetails videoDetails;
private YouTubePlayerSupportFragmentX youTubePlayerFragment;
//youtube player to play video when new video selected
private YouTubePlayer youTubePlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeYoutubePlayer();
setUpRecyclerView();
populateRecyclerView();
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView2 = findViewById(R.id.adView2);
AdRequest adRequest2 = new AdRequest.Builder().build();
mAdView2.loadAd(adRequest2);
}
private void setUpRecyclerView() {
firstrecyclerView = findViewById(R.id.first_recycler_view);
firstrecyclerView.setHasFixedSize(true);
videoDetailsoArrayList = new ArrayList<>();
displayVideos();
}
private void displayVideos() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
if (jsonObject1.has("id")) {
JSONObject jsonVideoId = jsonObject1.getJSONObject("id");
if (jsonVideoId.has("kind")) {
if (jsonVideoId.getString("kind").equals("youtube#video")) {
JSONObject jsonObjectSnippet = jsonObject1.getJSONObject("snippet");
JSONObject jsonObjectDefault = jsonObjectSnippet.getJSONObject("thumbnails").getJSONObject("medium");
String video_id = jsonVideoId.getString("videoId");
VideoDetails vd = new VideoDetails();
vd.setVideoId(video_id);
vd.setTitle(jsonObjectSnippet.getString("title"));
vd.setDescription(jsonObjectSnippet.getString("description"));
vd.setUrl(jsonObjectDefault.getString("url"));
videoDetailsoArrayList.add(vd);
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
firstrecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
firstrecyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext(), LinearLayoutManager.VERTICAL));
adapter = new adapter(getApplicationContext(), videoDetailsoArrayList);
firstrecyclerView.setAdapter(adapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), LENGTH_LONG).show();
}
});
requestQueue.add(stringRequest);
}
private void initializeYoutubePlayer() {
youTubePlayerFragment=(YouTubePlayerSupportFragmentX) getSupportFragmentManager().findFragmentById(R.id.youtube_player_fragment);
if (youTubePlayerFragment == null)
return;
youTubePlayerFragment.initialize(API_Key, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
youTubePlayer = player;
//set the player style default
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
//set the player style default
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
//cue the 1st video by default
// youTubePlayer.cueVideo(String.valueOf(videoDetailsoArrayList.get(0)));
youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
//print or show error if initialization failed
Log.e(TAG, "Youtube Player View initialization failed");
}
});
}
//POPULATE RECYCLER VIEW
private void populateRecyclerView() {
final adapter adapter = new adapter(this, videoDetailsoArrayList);
firstrecyclerView.setAdapter(adapter);
//set click event
firstrecyclerView.addOnItemTouchListener(new RecyclerViewOnClickListener(this, new RecyclerViewOnClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
if (youTubePlayerFragment != null && youTubePlayer != null) {
//update selected position
adapter.setSelectedPosition(position);
//load selected video
youTubePlayer.loadVideo(String.valueOf(videoDetailsoArrayList.get(position)));
// youTubePlayer.loadVideo(videoDetails.get(position)));
}
}
}));
}
}

而错误日志中的错误Cat在这里

2020-09-12 05:19:31.600 28102-28102/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2020-09-12 05:19:38.374 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:38.582 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:38.609 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:38.677 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:38.801 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:38.823 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatImageView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:38.839 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatImageView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:38.941 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:39.007 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatImageView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:39.015 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:39.043 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:39.050 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatImageView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:39.090 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:39.200 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:39.223 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:39.232 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:39.261 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatImageView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:39.270 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatImageView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:39.278 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
Theme.AppCompat theme (or descendant).
2020-09-12 05:19:39.312 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class Theme.AppCompat theme (or descendant).
2020-09-12 05:19:39.362 28102-28102/com.currentmedia.wasifaliwasif E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
2020-09-12 05:19:42.477 28102-28295/com.currentmedia.wasifaliwasif E/YouTubeAndroidPlayerAPI: Embed config is not supported in RemoteEmbeddedPlayer.
2020-09-12 05:19:43.145 28102-28271/com.currentmedia.wasifaliwasif E/YouTubeAndroidPlayerAPI: Embed config is not supported in RemoteEmbeddedPlayer.
2020-09-12 05:19:45.038 28102-28271/com.currentmedia.wasifaliwasif E/YouTubeAndroidPlayerAPI: Embed config is not supported in RemoteEmbeddedPlayer.

我曾尝试过用Base.Apptheme来更改Apptheme,如stackover回答中所述,但没有成功。该应用程序显示Youtube片段,使用缩略图和标题回收视图,但不响应点击事件。

您的应用程序有一个AppCompat主题

<application
android:theme="@style/AppTheme">

但是,您用一个不是AppCompat主题后代的主题覆盖了"活动"(扩展了AppCompatActivity(

你可以这样定义自己的全屏主题(注意父级中的AppCompat=(

<style name="AppFullScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>

然后在"活动"中设置。

<activity android:name=".MainActivity"
android:theme="@style/AppFullScreenTheme" >

最新更新