YouTubePlayerFragment无法转换为android.support.v4.app.Fragment是问题所在
编辑问题:我已经更新了代码,因为我使用了YouTubePlayerSupportFragment而不是YouTubePlayerFragment,我在主要活动中的displayVIEw方法中遇到错误(案例4:fragment = new Youtube(); break;)说无法从Youtube转换为片段,我无法得到一个logcat,因为我甚至无法运行它
编辑问题2:代码再次更新,但现在我有运行时问题,继承logcat
07-22 22:15:09.782: E/AndroIDRuntime(20603): FATAL EXCEPTION: main07-22 22:15:09.782: E/AndroIDRuntime(20603): Process: net.frankanDWalters,PID: 2060307-22 22:15:09.782: E/AndroIDRuntime(20603): java.lang.NullPointerException: Attempt to invoke virtual method 'voID com.Google.androID.youtube.player.YouTubePlayerVIEw.a()' on a null object reference07-22 22:15:09.782: E/AndroIDRuntime(20603): at com.Google.androID.youtube.player.YouTubePlayerSupportFragment.onStart(UnkNown Source)07-22 22:15:09.782: E/AndroIDRuntime(20603): at androID.support.v4.app.Fragment.performStart(Fragment.java:1502)07-22 22:15:09.782: E/AndroIDRuntime(20603): at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:957)07-22 22:15:09.782: E/AndroIDRuntime(20603): at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1104)07-22 22:15:09.782: E/AndroIDRuntime(20603): at androID.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)07-22 22:15:09.782: E/AndroIDRuntime(20603): at androID.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)07-22 22:15:09.782: E/AndroIDRuntime(20603): at androID.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:440)07-22 22:15:09.782: E/AndroIDRuntime(20603): at androID.os.Handler.handleCallback(Handler.java:739)07-22 22:15:09.782: E/AndroIDRuntime(20603): at androID.os.Handler.dispatchMessage(Handler.java:95)07-22 22:15:09.782: E/AndroIDRuntime(20603): at androID.os.Looper.loop(Looper.java:135)07-22 22:15:09.782: E/AndroIDRuntime(20603): at androID.app.ActivityThread.main(ActivityThread.java:5351)07-22 22:15:09.782: E/AndroIDRuntime(20603): at java.lang.reflect.Method.invoke(Native Method)07-22 22:15:09.782: E/AndroIDRuntime(20603): at java.lang.reflect.Method.invoke(Method.java:372)07-22 22:15:09.782: E/AndroIDRuntime(20603): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)07-22 22:15:09.782: E/AndroIDRuntime(20603): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:703)
继承了java代码,希望有人能帮忙,谢谢
import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.Toast;import com.Google.androID.youtube.player.YouTubeInitializationResult;import com.Google.androID.youtube.player.YouTubePlayer;import com.Google.androID.youtube.player.YouTubePlayerSupportFragment;public class Youtube extends YouTubePlayerSupportFragment implements YouTubePlayer.OnInitializedListener { public static Youtube newInstance() { Youtube fragment = new Youtube(); return fragment; } private voID init(){ initialize("AIzaSyBybxmsBPvhMKgM4RKqpneb4Tf4yObVAkY",this); } public Youtube() { } @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { VIEw vIEw = inflater.inflate(R.layout.youtubefragment,container,false); Youtube ytf = newInstance(); ytf.init(); getActivity().getSupportFragmentManager().beginTransaction() .add(R.ID.youtube_player,ytf) .commit(); return vIEw; } @OverrIDe public voID onAttach(Activity activity) { super.onAttach(activity); } @OverrIDe public voID onDetach() { super.onDetach(); } @OverrIDe public voID onInitializationSuccess(YouTubePlayer.ProvIDer provIDer,YouTubePlayer player,boolean wasRestored) { if (!wasRestored) { player.cueVIDeo("nCgQDjiotG0"); } } @OverrIDe public voID onInitializationFailure(YouTubePlayer.ProvIDer provIDer,YouTubeInitializationResult youTubeInitializationResult) { Toast.makeText(getActivity(),"Failured to Initialize!",Toast.LENGTH_LONG).show(); }}
这是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" androID:background="@drawable/background" androID:ID="@+ID/fragment_youtube_player_holder" ><fragment androID:name="com.Google.androID.youtube.player.YouTubePlayerSupportFragment" androID:ID="@+ID/youtube_player" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"/></linearLayout>
这是包含导航抽屉的主要活动
import java.util.ArrayList;import net.frankanDWalters.adapter.NavDrawerlistadapter;import net.frankanDWalters.model.NavDrawerItem;import androID.support.v4.app.Fragment;import androID.content.res.Configuration;import androID.content.res.TypedArray;import androID.os.Bundle;import androID.support.v4.app.ActionbarDrawerToggle;import androID.support.v4.app.FragmentActivity;import androID.support.v4.Widget.DrawerLayout;import androID.util.Log;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.Widget.AdapterVIEw;import androID.Widget.ListVIEw;public class MainActivity extends FragmentActivity { private DrawerLayout mDrawerLayout; private ListVIEw mDrawerList; private ActionbarDrawerToggle mDrawerToggle; // nav drawer Title //private CharSequence mDrawerTitle; // used to store app Title //private CharSequence mTitle; // slIDe menu items private String[] navMenuTitles; private TypedArray navMenuIcons; private ArrayList<NavDrawerItem> navDrawerItems; private NavDrawerlistadapter adapter; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); //mTitle = mDrawerTitle = getTitle(); // load slIDe menu items navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); // nav drawer icons from resources navMenuIcons = getResources() .obtainTypedArray(R.array.nav_drawer_icons); mDrawerLayout = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); mDrawerList = (ListVIEw) findVIEwByID(R.ID.List_slIDermenu); navDrawerItems = new ArrayList<NavDrawerItem>(); // adding nav drawer items to array // Home navDrawerItems.add(new NavDrawerItem(navMenuTitles[0],navMenuIcons.getResourceID(0,-1))); // Find People navDrawerItems.add(new NavDrawerItem(navMenuTitles[1],navMenuIcons.getResourceID(1,-1))); // Photos navDrawerItems.add(new NavDrawerItem(navMenuTitles[2],navMenuIcons.getResourceID(2,-1))); // CommunitIEs,Will add a counter here navDrawerItems.add(new NavDrawerItem(navMenuTitles[3],navMenuIcons.getResourceID(3,-1))); // Pages navDrawerItems.add(new NavDrawerItem(navMenuTitles[4],navMenuIcons.getResourceID(4,-1))); // What's hot,We will add a counter here navDrawerItems.add(new NavDrawerItem(navMenuTitles[5],navMenuIcons.getResourceID(5,-1))); navDrawerItems.add(new NavDrawerItem(navMenuTitles[6],navMenuIcons.getResourceID(6,-1))); navDrawerItems.add(new NavDrawerItem(navMenuTitles[7],navMenuIcons.getResourceID(7,-1))); navDrawerItems.add(new NavDrawerItem(navMenuTitles[8],navMenuIcons.getResourceID(8,-1))); navDrawerItems.add(new NavDrawerItem(navMenuTitles[9],navMenuIcons.getResourceID(9,-1))); navDrawerItems.add(new NavDrawerItem(navMenuTitles[10] + " disc 3",navMenuIcons.getResourceID(10,-1))); // Recycle the typed array navMenuIcons.recycle(); mDrawerList.setonItemClickListener(new SlIDeMenuClickListener()); // setting the nav drawer List adapter adapter = new NavDrawerlistadapter(getApplicationContext(),navDrawerItems); mDrawerList.setAdapter(adapter); // enabling action bar app icon and behaving it as toggle button getActionbar().setdisplayHomeAsUpEnabled(true); getActionbar().setHomebuttonEnabled(true); mDrawerToggle = new ActionbarDrawerToggle(this,mDrawerLayout,R.drawable.ic_drawer,//nav menu toggle icon R.string.app_name,// nav drawer open - description for accessibility R.string.app_name // nav drawer close - description for accessibility ) { public voID onDrawerClosed(VIEw vIEw) { //getActionbar().setTitle(mTitle); // calling onPrepareOptionsMenu() to show action bar icons invalIDateOptionsMenu(); } public voID onDrawerOpened(VIEw drawerVIEw) { //getActionbar().setTitle(mDrawerTitle); // calling onPrepareOptionsMenu() to hIDe action bar icons invalIDateOptionsMenu(); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); if (savedInstanceState == null) { // on first time display vIEw for first nav item displayVIEw(0); } } /** * SlIDe menu item click Listener * */ private class SlIDeMenuClickListener implements ListVIEw.OnItemClickListener { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent,VIEw vIEw,int position,long ID) { // display vIEw for selected nav drawer item displayVIEw(position); } } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main,menu); return true; } @OverrIDe public boolean onoptionsItemSelected(MenuItem item) { // toggle nav drawer on selecting action bar app icon/Title if (mDrawerToggle.onoptionsItemSelected(item)) { return true; } // Handle action bar actions click switch (item.getItemID()) { case R.ID.action_settings: return true; default: return super.onoptionsItemSelected(item); } } /* * * Called when invalIDateOptionsMenu() is triggered */ @OverrIDe public boolean onPrepareOptionsMenu(Menu menu) { // if nav drawer is opened,hIDe the action items boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); menu.findItem(R.ID.action_settings).setVisible(drawerOpen && !drawerOpen); return super.onPrepareOptionsMenu(menu); } /** * Diplaying fragment vIEw for selected nav drawer List item * */ private voID displayVIEw(int position) { // update the main content by replacing fragments Fragment fragment = null; switch (position) {// case 0:// fragment = new Home();// break;// case 1:// fragment = new Forum();// break;// case 2:// fragment = new Facebook();// break;// case 3:// fragment = new Twitter();// break; case 4: //fragment = Youtube.newInstance();// if(YouTubeIntents.isYouTubeInstalled(getBaseContext()) ) {// if(YouTubeAPIServiceUtil.isYouTubeAPIServiceAvailable(getBaseContext()) != YouTubeInitializationResult.SUCCESS) {// // fragment = new Youtube();// Intent intent = YouTubeIntents.createuserIntent(this,"FrankanDWalters");// startActivity(intent);// // } else { fragment = new Youtube(); //Intent intent = YouTubeIntents.createuserIntent(this,"FrankanDWalters"); // startActivity(intent);// }// } break;// case 5:// fragment = new Instagram();// break;// case 6:// fragment = new Google();// break;// case 7:// fragment = new Gigography();// break;// case 8:// fragment = new discography();// break;// case 9:// fragment = new Lyrics();// break;// case 10:// fragment = new Souvenirs();// break; default: break; } if (fragment != null) { getSupportFragmentManager().beginTransaction() .replace(R.ID.frame_container,fragment).commit(); // update selected item and Title,then close the drawer mDrawerList.setItemChecked(position,true); mDrawerList.setSelection(position); //setTitle(navMenuTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); } else { // error in creating fragment Log.e("MainActivity","Error in creating fragment"); } }// @OverrIDe// public voID setTitle(CharSequence Title) {// mTitle = Title;// getActionbar().setTitle("The Frank And Walters");// } /** * When using the ActionbarDrawerToggle,you must call it during * onPostCreate() and onConfigurationChanged()... */ @OverrIDe protected voID onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @OverrIDe public voID onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Pass any configuration change to the drawer toggls mDrawerToggle.onConfigurationChanged(newConfig); }}解决方法 将YouTubePlayerFragment替换为 YouTubePlayerSupportFragment,因为这是使用Fragment支持版本的版本. 总结
以上是内存溢出为你收集整理的YouTubePlayerFragment无法转换为android.support.v4.app.Fragment全部内容,希望文章能够帮你解决YouTubePlayerFragment无法转换为android.support.v4.app.Fragment所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)