我正在尝试在屏幕的某个标签页中加载YouTube视频。标签基本上是一个名为SocialFragmentTab
的片段。我在这里使用YouTubePlayerSupportFragment
。我正在获取应该在YouTube播放器中加载的YouTube视频的ID。但在运行代码时,我收到错误:
找不到ID为0x7f0c00f6的视图 (com.almaybay.almabay:id / youtube_fragment)片段 YouTubePlayerSupportFragment {860e2da#0 id = 0x7f0c00f6}
请帮我解决问题。代码是:
的 1。 SocialFragmentTab.java '
public class SocialFragmentTab extends Fragment {
Context context;
private static final String TAG = DashboardActivity.class.getSimpleName();
private ListView listView;
private FeedListSocialAdapter listAdapter;
private List<FeedItemSocial> feedItems;
//private String URL_FEED = "http://192.168.2.17:8000/api/v1/user-timeline-social/?access_token=yp1faO6Co6ISkrmO1nl1eUPMhjorua&user=491&type1=story&type2=story&start=0&limit=10&format=json";
private String URL_FEED_Social_Timeline = "";
AlmabayDatabase almabayDatabase;
APIConfiguration apiConfiguration;
int uid;
String accessToken, api;
private ConfigYoutubeAPI configYoutubeAPI;
private YouTubePlayer YPlayer;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_socialtab, container, false);
listView = (ListView) view.findViewById(R.id.list);
feedItems = new ArrayList<FeedItemSocial>();
listAdapter = new FeedListSocialAdapter(getActivity(), feedItems);
listView.setAdapter(listAdapter);
almabayDatabase = new AlmabayDatabase(getActivity());
// Getting userID from database
uid = almabayDatabase.getUserID();
//Getting accesss token from database
accessToken = almabayDatabase.getAccessToken();
apiConfiguration = new APIConfiguration();
api = apiConfiguration.getApi();
//Log.e("API", api);
URL_FEED_Social_Timeline = api + "/webservice/userSocialTimeline" + "?" + "user_id=" + uid + "&" + "access_token=" + accessToken + "&" + "before_post_id=1364&after_post_id=1354";
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Cache.Entry entry = cache.get(URL_FEED_Social_Timeline);
context = getActivity();
// making fresh volley request and getting json
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL_FEED_Social_Timeline, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray jsonArray) {
//Log.e("JSONArraySocial", String.valueOf(jsonArray));
longInfo(String.valueOf(jsonArray));
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// Log.e("Volley Test", String.valueOf(jsonObject.getInt("id")));
parseJsonFeed(jsonObject);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("Error Test", volleyError.getMessage());
}
});
// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonArrayRequest);
// Creates a youtube fragment
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();
configYoutubeAPI = new ConfigYoutubeAPI();
String YoutubeDeveloperKey = configYoutubeAPI.getYouTubeAPI();
Log.e("YouTubeDeveloperKey",YoutubeDeveloperKey);
// Initialize a youtube player which can be used to play videos and control video playback
youTubePlayerFragment.initialize(YoutubeDeveloperKey, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider arg0, YouTubePlayer youTubePlayer, boolean b) {
if (!b) {
YPlayer = youTubePlayer;
YPlayer.setFullscreen(false);
YPlayer.loadVideo(configYoutubeAPI.getYouTubeVideoID());
YPlayer.play();
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "Error in loading video", Toast.LENGTH_LONG).show();
}
});
return view;
}
public void parseJsonFeed(JSONObject jsonObject) {
//Log.e("JSONObjectSocial", String.valueOf(jsonObject));
FeedItemSocial item = new FeedItemSocial();
// Getting values from JSON data
try {
String text = jsonObject.getString("text");
String timestamp = jsonObject.getString("timestamp");
JSONObject jsonPublisher = jsonObject.getJSONObject("publisher");
String name = jsonPublisher.getString("name");
String media_type = jsonObject.getString("media_type");
if (media_type.equals("youtube")) {
JSONObject jsonMedia = jsonObject.getJSONObject("media");
String youtubeID = jsonMedia.getString("id");
Log.e("YoutubeID", youtubeID);
configYoutubeAPI = new ConfigYoutubeAPI();
configYoutubeAPI.setYouTubeVideoID(youtubeID);
}
String visitor_profilePic = jsonPublisher.getString("thumbnail_url");
//Adding values to the FeedItemSocial
item.setVisitor_status(text);
item.setTimeStamp(timestamp);
item.setVisitor_name(name);
item.setVisitor_profilePic(visitor_profilePic);
// Adding items to the list
feedItems.add(item);
// notify data changes to list adapater
listAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
// To display long String in the Logcat
public static void longInfo(String str) {
if (str.length() > 4000) {
Log.e("JSONArrayResponse", str.substring(0, 4000)); // Displaying substring from 0 to 3999.
longInfo(str.substring(4000)); // Substring from 4000 onwards
} else
Log.e("JSONArrayResponse", str);
}
}
2。 feed_item_social.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="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/bg_parent_rounded_corner"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="@dimen/feed_item_padding_left_right"
android:paddingRight="@dimen/feed_item_padding_left_right">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/profilePicVisitor"
android:layout_width="@dimen/feed_item_profile_pic"
android:layout_height="@dimen/feed_item_profile_pic"
android:scaleType="fitCenter"></com.android.volley.toolbox.NetworkImageView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="@dimen/feed_item_profile_info_padd">
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/feed_item_profile_name"
android:textStyle="bold" />
<TextView
android:id="@+id/timestamp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/timestamp"
android:textSize="@dimen/feed_item_timestamp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<ImageButton
android:id="@+id/imgLike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@drawable/likes_social" />
<TextView
android:id="@+id/txtLike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Like" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<ImageButton
android:id="@+id/imgShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@drawable/share2_social" />
<TextView
android:id="@+id/txtShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Share" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<ImageButton
android:id="@+id/imgNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@drawable/notification_social" />
<TextView
android:id="@+id/txtNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Get Notification" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/txtStatusMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="@dimen/feed_item_status_pad_left_right"
android:paddingRight="@dimen/feed_item_status_pad_left_right"
android:paddingTop="@dimen/feed_item_status_pad_top" />
<com.almaybay.almabay.FeedImageView
android:id="@+id/feedImage1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:scaleType="fitXY"
android:visibility="visible" />
<FrameLayout
android:id="@+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:visibility="visible" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/txtLikeCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="@drawable/likes_social" />
<TextView
android:id="@+id/txtCommentCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/txtShareCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="@drawable/share2_social" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
3.ConfigYoutubeAPI.java
public class ConfigYoutubeAPI {
String youTubeAPI = "AIzaSyD-6mroK72Y2Gst9lhzNS4vLzrQ2EaDh0I";
String youTubeVideoID;
public String getYouTubeAPI() {
return youTubeAPI;
}
public String getYouTubeVideoID() {
return youTubeVideoID;
}
public void setYouTubeVideoID(String youTubeVideoID) {
this.youTubeVideoID = youTubeVideoID;
}
}
4。 logcat的
Process: com.almaybay.almabay, PID: 31973
java.lang.IllegalArgumentException: No view found for id 0x7f0c00f6 (com.almaybay.almabay:id/youtube_fragment) for fragment YouTubePlayerSupportFragment{860e2da #0 id=0x7f0c00f6}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1018)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
at android.support.v4.app.Fragment.performStart(Fragment.java:1983)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:545)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
at android.view.View.measure(View.java:17429)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17429)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:940)
at android.view.View.measure(View.java:17429)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:17429)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17429)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17429)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17429)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2815)
at android.view.View.measure(View.java:17429)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5786)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.