Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用片段从Imageview到VideoView_Java_Android_Android Fragments - Fatal编程技术网

Java 使用片段从Imageview到VideoView

Java 使用片段从Imageview到VideoView,java,android,android-fragments,Java,Android,Android Fragments,我正在尝试使用fragment制作一个带有导航抽屉的应用程序,在主屏幕上应该有图像,当您单击图像时,它会将您带到特定图像的片段,视频开始播放。但不幸的是,我在主屏幕上看不到任何图像(也许我在布局上犯了错误)。请帮帮我。提前谢谢 代码如下:- 主xml文件 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schema

我正在尝试使用fragment制作一个带有导航抽屉的应用程序,在主屏幕上应该有图像,当您单击图像时,它会将您带到特定图像的片段,视频开始播放。但不幸的是,我在主屏幕上看不到任何图像(也许我在布局上犯了错误)。请帮帮我。提前谢谢

代码如下:-

主xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
      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"
      android:id="@+id/drawer_layout"
      android:fitsSystemWindows="true"
      tools:context="com.hangout.google.heedbasketball.MainActivity"
      tools:openDrawer="start"
      tools:deviceIds= "tv"
      tools:ignore="MergeRootFrame">

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">



      <android.support.v7.widget.Toolbar
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:background="@color/colorPrimaryDark"
          android:id="@+id/toolbar"
          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
          app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
          android:elevation="4dp"/>

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

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

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

      <FrameLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/fragement_container" />




  </LinearLayout>

      <android.support.design.widget.NavigationView
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:id="@+id/nav_view"
          app:headerLayout="@layout/nav_header"
          app:menu="@menu/drawer_menu"/>



  </android.support.v4.widget.DrawerLayout>
这里是一个视频的java片段,我也用同样的方式定义了其他视频

package com.hangout.google.heedbasketball;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.VideoView;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link FirstFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link FirstFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FirstFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public FirstFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FirstFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static FirstFragment newInstance(String param1, String param2) {
        FirstFragment fragment = new FirstFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

//My video code here
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            VideoView v1 = (VideoView) findViewById(R.id.v1);
            v1.setVideoURI(Uri.parse("https://myvideo.mp4"));
            v1.start();
            v1.requestFocus();
            v1.setKeepScreenOn(true);


        }
    }

    private Object findViewById(int v1) {
        return 0;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false);

    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }


        }

xml file fragment for the video

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FirstFragment"
    android:background="@color/colorPrimaryDark">

    <!-- TODO: Update blank fragment layout -->

<VideoView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/v1" />

</RelativeLayout>
package com.hangout.google.heedbasketball;
导入android.content.Context;
导入android.net.Uri;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentManager;
导入android.support.v4.app.FragmentPagerAdapter;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.VideoView;
/**
*一个简单的{@link Fragment}子类。
*包含此片段的活动必须实现
*{@link FirstFragment.OnFragmentInteractionListener}接口
*处理交互事件。
*使用{@link FirstFragment#newInstance}工厂方法
*创建此片段的实例。
*/
公共类FirstFragment扩展了Fragment{
//TODO:重命名参数参数,选择匹配的名称
//片段初始化参数,例如ARG_ITEM_NUMBER
私有静态最终字符串ARG_PARAM1=“PARAM1”;
私有静态最终字符串ARG_PARAM2=“PARAM2”;
//TODO:重命名和更改参数类型
私有字符串mParam1;
私有字符串mParam2;
私有OnFragmentInteractionListener mListener;
公共第一片段(){
//必需的空公共构造函数
}
/**
*使用此工厂方法创建的新实例
*使用提供的参数创建此片段。
*
*@param param1参数1。
*@param param2参数2。
*@return fragment FirstFragment的新实例。
*/
//TODO:重命名和更改参数的类型和数量
公共静态FirstFragment newInstance(字符串param1,字符串param2){
FirstFragment=新的FirstFragment();
Bundle args=新Bundle();
args.putString(ARG_PARAM1,PARAM1);
args.putString(ARG_PARAM2,PARAM2);
fragment.setArguments(args);
返回片段;
}
//我的视频代码在这里
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(getArguments()!=null){
mParam1=getArguments().getString(ARG_PARAM1);
VideoView v1=(VideoView)findviewbyd(R.id.v1);
v1.setVideoURI(Uri.parse(“https://myvideo.mp4"));
v1.start();
v1.requestFocus();
v1.setKeepScreenOn(真);
}
}
私有对象findViewById(int v1){
返回0;
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
//为该碎片膨胀布局
返回充气机。充气(R.layout.fragment_first,container,false);
}
//TODO:重命名方法、更新参数并将方法挂接到UI事件中
public void onButtonPressed(Uri){
if(mListener!=null){
onFragmentInteraction(uri);
}
}
@凌驾
公共void-onAttach(上下文){
super.onAttach(上下文);
if(OnFragmentInteractionListener的上下文实例){
mListener=(OnFragmentInteractionListener)上下文;
}否则{
抛出新的RuntimeException(context.toString()+“必须实现OnFragmentInteractionListener”);
}
}
@凌驾
公共无效连接(){
super.onDetach();
mListener=null;
}
/**
*此接口必须由包含以下内容的活动实现
*片段,以允许通信此片段中的交互
*该活动以及其中可能包含的其他片段
*活动。
*
*有关更多信息,请参阅Android培训课程。
*/
FragmentInteractionListener上的公共接口{
//TODO:更新参数类型和名称
void onFragmentInteraction(Uri);
}
}
视频的xml文件片段

您已经在创建时创建了findViewById函数,该函数返回0

您需要从正在充气的布局中获取视频参考。 类似的东西

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                          Bundle savedInstanceState) {
    // Inflate the layout for this fragment
     View view  =  inflater.inflate(R.layout.fragment_first, container, false);
        // see the difference
        VideoView v1 = (VideoView) view.findViewById(R.id.v1);
        v1.setVideoURI(Uri.parse("https://myvideo.mp4"));
        v1.start();
        v1.requestFocus();
        v1.setKeepScreenOn(true);    
     return view;
}

我能知道,为什么你建议编辑几乎所有的文件,除了片段?我试过用你的方式,但现在我的应用程序甚至无法打开。
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                          Bundle savedInstanceState) {
    // Inflate the layout for this fragment
     View view  =  inflater.inflate(R.layout.fragment_first, container, false);
        // see the difference
        VideoView v1 = (VideoView) view.findViewById(R.id.v1);
        v1.setVideoURI(Uri.parse("https://myvideo.mp4"));
        v1.start();
        v1.requestFocus();
        v1.setKeepScreenOn(true);    
     return view;
}