【问题标题】:Error loading youtube video: Unknown youtube Activity加载 youtube 视频时出错:未知的 youtube 活动
【发布时间】:2020-12-30 11:47:45
【问题描述】:

我使用 onClick 监听器的代码部分没有响应,也没有播放任何视频。


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

            }
        }));
    }
}


而error logCat中的错误就在这里

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.

我已尝试按照 stackover 答案中的描述使用 Base.AppTheme 更改 Apptheme,但它不起作用。该应用正在显示 Youtube 片段、带有缩略图和标题的 recyclerview,但没有响应点击事件。

【问题讨论】:

    标签: java android-studio android-activity youtube-api onclicklistener


    【解决方案1】:

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

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

    但是,您使用不是 AppCompat 主题后代的主题覆盖了 Activity(它扩展了 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 上设置它。

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

    【讨论】:

      猜你喜欢
      • 2012-07-09
      • 2014-07-06
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 2014-04-02
      • 2017-03-30
      相关资源
      最近更新 更多