2011-06-21 15 views
0

3つの異なるアクティビティに関連付けられた3つのタブを持つアプリケーションでタブホストを使用しています。それぞれのアクティビティの中で、フラグメントを追加する必要があります(フラグメントには、Webからビデオを再生するビデオプレーヤーが含まれています)。以下は、フラグメントを追加するアクティビティのコードです。TabHost内のフラグメントアクティビティ

 


    public class FilmsActivity extends Activity { 

     @SuppressWarnings("unused") 
     private static final String TAG = "FilmsActivity"; 

     private static FragmentManager mFragmentManager; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      Log.i(TAG, "Films onCreate "); 
      mFragmentManager = getFragmentManager(); 
      FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); 
      // get a video fragment. Title and URI as the constructor arguments 
      VideoViewer vvFragment = VideoViewer.newInstance("Testing",Uri.parse("http://daily3gp.com/vids/747.3gp")); 
      Log.i(TAG, "Films onCreate 1"); 
      VideoViewer vvFragment = VideoViewer.newInstance("Testing",Uri.parse("/sdcard/family_guy_test.3gp")); 
      Log.i(TAG, "Films onCreate 2"); 
      fragmentTransaction.add(android.R.id.tabcontent, vvFragment, "videoFragment"); 
      Log.i(TAG, "Films onCreate 3"); 
      fragmentTransaction.commit(); 
     } 
    } 

以下に示すような断片の実装です。

 


     public class VideoViewer extends Fragment { 
     // Debug vars 
     @SuppressWarnings("unused") 
     private static final String TAG = "VideoViewer"; 
     private static final Boolean DBG_TOAST = false; 
     private static final Boolean DBG_LOG = false; 

     // Video Title, URL of the Video will be unique for each video. Save it in a member variable. 
     private static final String VIDEO_TITLE = "title"; 
     private static final String VIDEO_URI = "uri"; 
     // Different states of the video viewer 
     private static final int STATE_PAUSED = 0; 
     private static final int STATE_PLAYING = 1; 
     private static final int STATE_STOPPED = 2; 
     // The view which holds the video player 
     private VideoView mVideoView; 
     // The transparent layout above the video which will be shown when it is paused and made not 
     // visible when the video is playing 
     // The title of the video is in the textview 
     private TextView mVideoTitle; 
     // UI Control Elements from the view. 
     private ImageButton mPlayPauseButton; 
     private Button mShareButton; 
     private Button mAddToFavourites; 
     // This holds the current state of the Video Viewer 
     private int mVideoState; 

     public static VideoViewer newInstance(String VideoTitle, Uri VideoUri) { 
      VideoViewer VV = new VideoViewer(); 
      Bundle args = new Bundle(); 
      // Put the title and the uri in the bundle 
      args.putString(VIDEO_TITLE, VideoTitle); 
      args.putString(VIDEO_URI, VideoUri.toString()); 
      // Later on convert the string to Uri using uri.parse(string) 
      return VV; 
     } 

     /* Return the view that is used by the fragment */ 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      // Check if you want to use container or don't want to use anything 
      return inflater.inflate(R.layout.fragment_video_viewer, null); 
     } 

     /* Initialize the variables here, retrieve views and */ 
     @Override 
     public void onActivityCreated(Bundle savedInstanceState) { 
      super.onActivityCreated(savedInstanceState); 
      // Assignment for the local variables 
      mVideoView = (VideoView) getView().findViewById(R.id.fragment_video_viewer_video); 
      mVideoTitle = (TextView) getView().findViewById(R.id.fragment_video_viewer_title); 
      // Use the video title as the Text in the TextView 
      mVideoTitle.setText(getArguments().getString(VIDEO_TITLE)); 
      // Get the URL from the argument bundle 
      Uri videoUri = Uri.parse(getArguments().getString(VIDEO_URI)); 
      videoUri = Uri.parse("http://daily3gp.com/vids/747.3gp"); 
      // Toast if the URI is null 
      if(DBG_TOAST) { 
       if (videoUri.toString() == "") { 
        // Tell the user to provide a media file URL/path. 
        Toast.makeText(getActivity().getApplicationContext(), "Uri is null", Toast.LENGTH_LONG).show(); 
        // Use a default video 
        // videoUri = Uri.parse("http://daily3gp.com/vids/747.3gp"); 
       } 
      } 
      // Initialize a media controller and anchor the videoview to it 
      MediaController mediaController = new MediaController(getActivity().getApplicationContext()); 
      mediaController.setAnchorView(mVideoView); 

      mVideoView.setMediaController(mediaController); 
      mVideoView.requestFocus(); 
      mVideoView.setVideoURI(videoUri); 
      mVideoView.start(); 
     } 
    } 

 

実装で何が間違っているか教えていただけますか?空のFilmsアクティビティは、3つのタブを持つ画面を期待通りに作成します。しかし、私はこの実装で実行しようとすると、アプリはまったく実行されていません。

フィルムアクティビティは、タブアクティブを生成するメインアクティビティから呼び出されます。したがって、映画活動の見解はandroid.R.id.tabcontentになるはずです。私はフラグメントを追加するときにそれを使うべきですか?

答えて

2

問題が解決しました。 Activityの代わりにFragmentActivityを延長しなければならなかった。

関連する問題