在Android中使用Firebase和RecyclerView在一项活动中同时使用AppCompat和Youtube API [英] Using both AppCompat and Youtube API in one activity with firebase and RecyclerView in Android
问题描述
我正在开发一个应同时使用 AppCompatActivity 和 Youtube API 的应用程序,但是由于某些原因,当我在同一活动中无法同时使用两者时对我的工具栏使用AppCompatActicity, Youtube API停止工作;如果我扩展了YoutubeBaseActivity,工具栏不起作用,那么我也在使用RecyclerView
和 Firebase 作为此应用程序中的数据库,任何人都可以帮助我,并告诉我如何一起实现AppCompatActivity和youtubeAPI,以便它们都能工作.我研究了很多论坛,但他们没有提供任何有用的信息,请帮忙.
I am working on an application in which both AppCompatActivity and Youtube API should be used, but for some reasons I am unable to use both together in same activity when I use AppCompatActicity for my toolbar the Youtube API stops working and if I extends YoutubeBaseActivity the Toolbar does not work, I am also using RecyclerView
and Firebase as a database in this applcation, can anybody help me with it and tell me how I can implement AppCompatActivity and youtubeAPI together so they both work, I have researched many forums but they did not provide any useful information on doing so, please help.
我正在使用Android Studio
I am using Android Studio
Youtube类文件:
public class HomePage extends YouTubeBaseActivity{
private RecyclerViewPager mrecyclerView;
private DatabaseReference mDatabaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("holla");
mrecyclerView = (RecyclerViewPager) findViewById(R.id.list);
final LinearLayoutManager layout = new LinearLayoutManager(HomePage.this, LinearLayoutManager.VERTICAL, false);
mrecyclerView.setLayoutManager(layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mAuth = FirebaseAuth.getInstance();
mrecyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (getSupportActionBar().isShowing()) {
getSupportActionBar().hide();
fab.hide();
} else {
getSupportActionBar().show();
fab.show();
}
return true;
} else return false;
}
});
}
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter <post, postViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post, postViewHolder>(
post.class,
R.layout.post_row_recycle_home,
postViewHolder.class,
mDatabaseReference
) {
@Override
protected void populateViewHolder(postViewHolder viewHolder, post model, int position) {
viewHolder.setYoutube(model.getYoutube());
}
};
mrecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class postViewHolder extends RecyclerViewPager.ViewHolder{
View mView;
public postViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setYoutube(final String youtube){
final YouTubePlayerView youPlay = (YouTubePlayerView) mView.findViewById(R.id.youtuber);
youPlay.initialize("SOME KEY",
new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cueVideo(youtube);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult youTubeInitializationResult) {
}
});
}
Youtube CardView XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtuber"
android:layout_width="match_parent"
android:layout_height="200dp" />
记录错误:
E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{andromeda.petrochemical/andromeda.petrochemical.HomePage}: java.lang.IllegalArgumentException: No view found for id 0x7f10015f (andromeda.petrochemical:id/watch_video_player) for fragment YouTubePlayerSupportFragment{79044c4 #0 id=0x7f10015f}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f10015f (andromeda.petrochemical:id/watch_video_player) for fragment YouTubePlayerSupportFragment{79044c4 #0 id=0x7f10015f}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1293)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at andromeda.petrochemical.HomePage.onStart(HomePage.java:123)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
at android.app.Activity.performStart(Activity.java:6679)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
我想实现的目标
推荐答案
好的,您的常规HomePage
活动扩展了AppCompatActivity
OK you have your normal HomePage
activity that extend AppCompatActivity
在您的xml文件中,您像videoplayer
At your xml file you have like a container for the videoplayer
<FrameLayout
android:id="@+id/watch_video_player"
android:layout_width="match_parent"
android:layout_height="@dimen/watf_main_video" />
您的活动 添加播放器
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.watch_video_player, youTubePlayerFragment).commit();
它将获得youtube播放器,然后您将其正常初始化
it will get the youtube player then you initialize it as normal
youTubePlayerFragment.initialize(Constants.API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
//load and play the video with current id
youTubePlayer.loadVideo(YouTubeApiUtilities.getVideoId(item));
youTubePlayer.play();
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Toast.makeText(getContext(), R.string.can_not_initialize_youtube_player, Toast.LENGTH_LONG).show();
}
});
这篇关于在Android中使用Firebase和RecyclerView在一项活动中同时使用AppCompat和Youtube API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!