2015-09-27 96 views
1

我被困在試圖將我的片段中的內容放入android studio的主視圖中。無法將片段視圖切換到主視圖

該應用程序編譯並運行。但是,當我在導航抽屜中選擇一個項目(滑動菜單)到列表中帶有片段的項目時:它是空白的。

我的片段包含嵌入式YouTube播放器。

的logcat中不顯示錯誤...

從我的代碼 - 我是什麼俯瞰那是錯的?

這裏是主要活動:

import android.app.Activity; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.AppCompatActivity; 

import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 

public class MainActivity extends AppCompatActivity 
     implements NavigationDrawerFragment.NavigationDrawerCallbacks { 

    /** 
    * Fragment managing the behaviors, interactions and presentation of the navigation drawer. 
    */ 
    private NavigationDrawerFragment mNavigationDrawerFragment; 

    /** 
    * Used to store the last screen title. For use in {@link #restoreActionBar()}. 
    */ 
    private CharSequence mTitle; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mNavigationDrawerFragment = (NavigationDrawerFragment) 
       getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); 
     mTitle = getTitle(); 

     // Set up the drawer. 
     mNavigationDrawerFragment.setUp(
       R.id.navigation_drawer, 
       (DrawerLayout) findViewById(R.id.drawer_layout)); 
    } 

    @Override 
    public void onNavigationDrawerItemSelected(int position) { 
     // update the main content by replacing fragments 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction() 
       .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) 
       .commit(); 
    } 

    public void onSectionAttached(int number) { 
     switch (number) { 
      case 1: 
       mTitle = getString(R.string.title_section1); 
       break; 
      case 2: 
       mTitle = getString(R.string.title_section2); 
       break; 
      case 3: 
       mTitle = getString(R.string.title_section3); 
       break; 
      case 4: 
       mTitle = getString(R.string.title_section4); 
       break; 
      case 5: 
       mTitle = getString(R.string.title_section5); 
       break; 
     } 
    } 

    public void restoreActionBar() { 
     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
     actionBar.setDisplayShowTitleEnabled(true); 
     actionBar.setTitle(mTitle); 
    } 

    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) { 
     if (!mNavigationDrawerFragment.isDrawerOpen()) { 
      // Only show items in the action bar relevant to this screen 
      // if the drawer is not showing. Otherwise, let the drawer 
      // decide what to show in the action bar. 
      getMenuInflater().inflate(R.menu.main, menu); 
      restoreActionBar(); 
      return true; 
     } 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     private static final String ARG_SECTION_NUMBER = "section_number"; 

     /** 
     * Returns a new instance of this fragment for the given section 
     * number. 
     */ 
     public static PlaceholderFragment newInstance(int sectionNumber) { 
      PlaceholderFragment fragment = new PlaceholderFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     public PlaceholderFragment() { 
     } 

     /* @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
      return rootView; 
     } */ 

     @Override 
     public void onAttach(Activity activity) { 
      super.onAttach(activity); 
      ((MainActivity) activity).onSectionAttached(
        getArguments().getInt(ARG_SECTION_NUMBER)); 
     } 
    } 

} 

這裏是片段類:

import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.google.android.youtube.player.YouTubeInitializationResult; 
import com.google.android.youtube.player.YouTubePlayer; 
import com.google.android.youtube.player.YouTubePlayerView; 

public class MasjidAlHaramFragment extends Fragment implements YouTubePlayer.OnInitializedListener { 

    public String API_KEY = "xxxxxxxxxxxxxxxxxxxxx"; 

    //http://youtu.be/<VIDEO_ID> 
    //final String VIDEO_ID = "4OoKpZWJASY"; 
    public String VIDEO_ID_2 = "VopbGPJVkzM"; 

    public MasjidAlHaramFragment() { 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.fragment_masjidharam, container, false); 

     //http://youtu.be/<VIDEO_ID> 
     //final String VIDEO_ID = "4OoKpZWJASY"; 
     //final String VIDEO_ID_2 = "VopbGPJVkzM"; 

     /** Initializing YouTube player view **/ 
     YouTubePlayerView youTubePlayerView = null; 
     if (youTubePlayerView != null) 
      youTubePlayerView = (YouTubePlayerView) getView().findViewById(R.id.youtube_player); 
     youTubePlayerView.initialize(API_KEY, this); 

     return rootView; 
    } 

    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      /** attaching layout xml **/ 
      //setContentView(R.layout.activity_main); 

     } 

    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) { 
      //Toast.makeText(this,"Failured to Initialize!", Toast.LENGTH_LONG).show(); 
     } 

     public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) { 
      /** add listeners to YouTubePlayer instance **/ 
      player.setPlayerStateChangeListener(playerStateChangeListener); 
      player.setPlaybackEventListener(playbackEventListener); 

      /** Start buffering **/ 
      if (!wasRestored) { 
       player.cueVideo(VIDEO_ID_2); 
      } 
     } 

     YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() { 

      @Override 
      public void onBuffering(boolean arg0) { 
      } 

      @Override 
      public void onPaused() { 
      } 

      @Override 
      public void onPlaying() { 
      } 

      @Override 
      public void onSeekTo(int arg0) { 
      } 

      @Override 
      public void onStopped() { 
      } 

     }; 

     YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() { 

      @Override 
      public void onAdStarted() { 
      } 

      @Override 
      public void onError(YouTubePlayer.ErrorReason arg0) { 
      } 

      @Override 
      public void onLoaded(String arg0) { 
      } 

      @Override 
      public void onLoading() { 
      } 

      @Override 
      public void onVideoEnded() { 
      } 

      @Override 
      public void onVideoStarted() { 
      } 
     }; 
} 

,這裏是XML視圖

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <com.google.android.youtube.player.YouTubePlayerView 
     android:id="@+id/youtube_player" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#fff" 
     android:padding="5dp" /> 

</RelativeLayout> 

感謝。

ironmantis7x

回答

1

你需要調用super.onCreateView(...),在該片段中的onCreateView方法,才能正確得出的片段。

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    super.onCreateView(inflater, container, savedInstanceState); 

另外,我建議做以下,因爲有幾個邏輯錯誤:

View rootView = inflater.inflate(R.layout.fragment_masjidharam, container, false); 

YouTubePlayerView youTubePlayerView = null; 
// THIS IS ALWAYS FALSE. Comment it out or else it'll never get set. 
// if (youTubePlayerView != null) 

youTubePlayerView = (YouTubePlayerView) rootView.findViewById(R.id.youtube_player); 

// Check for not-null here. 
if(youTubePlayerView != null) { 
    youTubePlayerView.initialize(API_KEY, this); 
} 
+0

我會嘗試了這一點。你指出了我在我的項目中會遇到的一些嚴重缺陷,我會解決的。謝謝。 – ironmantis7x