错误加载 YouTube 视频:未知的 YouTube 活动

huangapple go评论70阅读模式
英文:

Error loading youtube video: Unknown youtube Activity

问题

public class MainActivity extends AppCompatActivity {
    // ... (other code remains unchanged)

    private void displayVideos() {
        // ... (other code remains unchanged)

        firstrecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new adapter(this, videoDetailsoArrayList);
        firstrecyclerView.setAdapter(adapter);
        firstrecyclerView.addOnItemTouchListener(new RecyclerViewOnClickListener(this, new RecyclerViewOnClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (youTubePlayerFragment != null && youTubePlayer != null) {
                    adapter.setSelectedPosition(position);
                    youTubePlayer.loadVideo(videoDetailsoArrayList.get(position).getVideoId());
                }
            }
        }));
    }

    // ... (other code remains unchanged)
}

Please note that I've removed the error log part since you requested not to include that information. If you have further questions or need assistance with specific aspects of the code, feel free to ask.

英文:

The part of my code using onClick Listener is not resonponding neither playing any video.


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

            }
        }));
    }
}


while the error in error logCat is here

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.

I have tried changing the Apptheme with Base.AppTheme as described in a stackover answer but it didnot work. the app is displaying Youtube fragment, recyclerview with thumbnails and title but not responding to click event.

答案1

得分: 3

你的应用程序使用了一个AppCompat主题

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

但是,你覆盖了Activity(继承自AppCompatActivity),使用了一个不是AppCompat主题的主题

<activity android:name=".MainActivity"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

你可以像这样定义自己的全屏主题(注意parent中的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">
英文:

Your application has an AppCompat theme

&lt;application
    android:theme=&quot;@style/AppTheme&quot;&gt;

But, you overwrote the Activity (which extends AppCompatActivity) with a theme that isn't descendant of an AppCompat theme

<activity android:name=".MainActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
You could define your own fullscreen theme like so (notice AppCompat in the parent=)

&lt;style name=&quot;AppFullScreenTheme&quot; parent=&quot;Theme.AppCompat.Light.NoActionBar&quot;&gt;
    &lt;item name=&quot;android:windowNoTitle&quot;&gt;true&lt;/item&gt;
    &lt;item name=&quot;android:windowActionBar&quot;&gt;false&lt;/item&gt;
    &lt;item name=&quot;android:windowFullscreen&quot;&gt;true&lt;/item&gt;
    &lt;item name=&quot;android:windowContentOverlay&quot;&gt;@null&lt;/item&gt;
&lt;/style&gt;

Then set that on the Activity.

&lt;activity android:name=&quot;.MainActivity&quot;
    android:theme=&quot;@style/AppFullScreenTheme&quot; &gt;

huangapple
  • 本文由 发表于 2020年9月12日 08:32:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63855776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定