YouTubePlayerSupportFragment
アイテムで aを使用しようとしていListView
ます。ListView
は の中にFragment
ありActivity
ます。ネストされたフラグメントがあるため、getChildFragmentManager()
メソッドを使用して XML レイアウトからフラグメントを見つけようとしています。これがコードです。
ジャワ
convertView = inflater.inflate(R.layout.youtube_post_layout, null);
YouTubePlayerSupportFragment youTubePlayerFragment = (YouTubePlayerSupportFragment) getChildFragmentManager().findFragmentById(R.id.youtube_video);
if (youTubePlayerFragment == null) {
Log.i("YouTube", "player is null");
} else {
Log.i("YouTube", youTubePlayerFragment.toString());
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<include layout="@layout/post_base_layout" />
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youtube_video"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginLeft="62dp"
android:layout_marginRight="32dp"
android:background="@color/light_grey"
android:cropToPadding="true"
android:padding="1dp"
android:scaleType="centerCrop" />
<include layout="@layout/post_bottom_layout" />
</LinearLayout>
findFragmentById()
問題は、XML から取得してフラグメントを作成しようとすると、 null が返され、それがスタック トレースにポストされることです。
YouTube API サンプルに従って、ネストされたフラグメントに必要な変更を加えようとしていますが、問題の原因がわかりません。