Android 如何调用主活动中的片段通过图像播放视频

Android 如何调用主活动中的片段通过图像播放视频,android,android-fragments,Android,Android Fragments,我正在尝试制作一个应用程序,在其中我可以点击一个图像,它应该在fragment的帮助下打开视频。但是我对如何在主java中调用片段感到困惑。请随时提供更多建议和更正 我的xml文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http

我正在尝试制作一个应用程序,在其中我可以点击一个图像,它应该在fragment的帮助下打开视频。但是我对如何在主java中调用片段感到困惑。请随时提供更多建议和更正

我的xml文件

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="horizontal">

        //image view as per the frame required
        <ImageView
            android:id="@+id/i1"
            android:src="@drawable/arena_reaction"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@android:color/background_dark" />

        <ImageView
            android:id="@+id/i2"
            android:src="@drawable/frequentflyer_diop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <ImageView
            android:id="@+id/i3"
            android:src="@drawable/frequentflyer_tyus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</RelativeLayout>

为片段创建基类,并在所有片段中扩展该类

公共抽象类BaseFragment扩展了Fragment{
公共静态BaseFragment newInstance(){}
}

然后在<代码>中单击 更改
片段myFragement=null
to
BaseFragment myFragment=null并将其赋值
myFragment=FirstVideo.newInstance(),myFragment=ThirdVideo.newInstance()


当您尝试上述操作时发生了什么?欢迎使用SO。请参阅如何提出可以有效回答的问题。@LarsH在onItemClick之后,我遇到了一个错误,好像您需要在此处使用分号,而我的碎片不被接受。我已经为我的所有碎片创建了基类,但我会尝试您的建议,然后返回给您。。顺便问一下,你对切换有什么建议?你能告诉我在哪里以及如何通过url调用我的视频视图吗?url和视频视图位于哪里?我为每个视频视图创建了不同的片段,并在那里声明了它们
package com.video.rsingh;
import android.app.Activity;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        ImageView i1 = (ImageView) findViewById( R.id.i1 ) ;
        ImageView i2 = (ImageView) findViewById( R.id.i2 ) ;
        ImageView i3 = (ImageView) findViewById( R.id.i3 ) ;
    }

    // **Issue after this code, I am trying to call the fragements here**

    { //**How to call here**
        public void OnItemClick(ImageView) {
            Fragment myFragement = null;
            Class fragementClass;
            switch (menuItem.getItemId()) {
                case R.id.i1:
                    fragementClass = FirstVideo.class;
                    break;
                case R.id.i2:
                    fragementClass = SecondFragement.class;
                    break;
                case R.id.i3:
                    fragementClass = ThirdVideo.class;
                    break;
                default:
                    fragementClass = FirstVideo.class;
            }

            **using try and catch here**
            try {
                myFragement = (Fragment) fragementClass.newInstance();
            }
            catch (Exception e) {
                e.printStackTrace();
            }

            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace( R.id.myFragement );
        }
    }
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace( R.id.containerIdInLayout, myFragment);