我创建了一个视频播放列表,显示存储在SD卡中的视频文件的名称。默认情况下,视频视图的可见性“消失”。一旦我从播放列表中选择视频,我希望视频视图变得可见并播放视频。但无法做到这一点。
这是我的代码:
VideoFragment.java
public class VideoFragment extends Fragment implements OnClickListener, OnCompletionListener {
ListView list;
ArrayAdapter<String> adapter ;
VideoListAdapter listAdapter;
ArrayList<String> listTest;
ArrayList<String> listSoundNames;
ImageButton play,stop,back,next;
String songpath,song,title;
int index,current_position;
File[] listFile;
SharedPreferences sharedPref;
MediaPlayer mp,mp2;
ActionBar bar;
private Boolean state=false;
private static int save = -1;
int count=0;
private static final String TAG = VideoFragment.class.getSimpleName();
//private Context _context = this;
public static int selected_item=-1;
VideoView video;
MediaController controller;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_games, container, false);
//controller = new MediaController(getActivity().getApplicationContext());
video = (VideoView) rootView.findViewById(R.id.video1);
//video.setVisibility(View.GONE);
list = (ListView)rootView.findViewById(R.id.list);
//list.setTextFilterEnabled(true);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// _context=this;
listTest = new ArrayList<String>( );
listSoundNames=new ArrayList<String>();
play = (ImageButton)rootView.findViewById(R.id.play);
back = (ImageButton)rootView.findViewById(R.id.prev);
next = (ImageButton)rootView.findViewById(R.id.next);
//adding listeners
play.setOnClickListener(this);
back.setOnClickListener(this);
next.setOnClickListener(this);
//action bar controls
bar = getActivity().getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#DF0174")));
//bar.setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
EditText editText = new EditText(getActivity().getApplicationContext());
getActivity().getActionBar().setCustomView(editText);
Scanner("/sdcard/");//storage path
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////*Adding listener to songs*//////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(listTest.size() != 0)
{
listAdapter=new VideoListAdapter(getActivity().getApplicationContext(),listSoundNames);
list.setAdapter(listAdapter);
list.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//////////////////changing list item background on click///////////////////
//list.setSelection(position);
//view.setSelected(true);
///////////////////PROBLEM/////////////
for(int a = 0; a < parent.getChildCount(); a++)
{
list.clearChoices();
parent.getChildAt(a).setBackgroundColor(Color.BLACK);
}
selected_item=position;
// view.setBackgroundColor(Color.RED);
////////////////////////////////////////////////////////////////////////////
//accessing song path
list.setItemChecked(position, true);
list.setSelection(position);
//accessing the song name
String name = (String) ((TextView) view).getText();
title = name;
//bar.setTitle(title);
//Log.e(TAG, name);
Toast.makeText(getActivity().getApplicationContext(), name, Toast.LENGTH_SHORT).show();
try{
//video.setMediaController(controller);
video.setVideoPath(listTest.get(position));//source
video.setVisibility(view.VISIBLE);
video.requestFocus();
video.start();
index = position;
play.setImageResource(R.drawable.pause);
}
catch(Exception e){e.printStackTrace();}
}
});
}
return rootView;
}
private void Scanner(String path) {
// TODO Auto-generated method stub
{
try
{
File fl = new File(path);
File[] listOfFiles = fl.listFiles();
for (File listOfFile : listOfFiles)
{
String s = listOfFile.getName();
if(s.endsWith(".mp4"))
{
songpath = listOfFile.getPath();
listTest.add(songpath);//adding song names to list
//listTest.toString().replaceFirst(songpath, s);
// store file name in listSoundNames
int pos = s.lastIndexOf(".");
if (pos > 0)
{
song = s.substring(0, pos);
}
listSoundNames.add(song);
}
/////////////////////////////////
File f = new File(path+s+"/");
if (f.exists() && f.isDirectory()) {
Scanner(path+s+"/");
}
////////////////////////////////
}
}
catch (Exception e) { }
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.equals(play))
{
if(mp.isPlaying())
{
mp.pause();
Toast.makeText(getActivity().getApplicationContext(), "paused", Toast.LENGTH_SHORT).show();
//change in button image//
play.setImageResource(R.drawable.play);
}
else
{
mp.start();
Toast.makeText(getActivity().getApplicationContext(), "started", Toast.LENGTH_SHORT).show();
//change in button image//
play.setImageResource(R.drawable.pause);
//
}
}
if (v.equals(back))
{
mp.stop();
mp.reset();
//bar.setTitle(song);
if(index!=0)
{
index = index -1;
selected_item=index;
listAdapter.notifyDataSetChanged();//shifting the highlight to the song played
}
else
{
index = (list.getAdapter().getCount()-1)-1;
selected_item=index;
listAdapter.notifyDataSetChanged();
}
Uri uri = Uri.parse(listTest.get(index).toString());//getting the path of next song
try {
mp.setDataSource(getActivity().getApplicationContext(), uri);//setting new data source
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getActivity().getApplicationContext(), "ERROR", Toast.LENGTH_SHORT).show();///PROBLEM:MOVING HERE AFTER CLICKING NEXT BUTTON
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
Toast.makeText(getActivity().getApplicationContext(), ""+uri, Toast.LENGTH_SHORT).show();
}
if (v.equals(next))
{
mp.stop();
mp.reset();
index = index +1;
selected_item=index;
listAdapter.notifyDataSetChanged();
//Toast.makeText(MP3.this, ""+selected_item, Toast.LENGTH_SHORT).show();
Uri uri = Uri.parse(listTest.get(index).toString());//getting the path of next song
try {
mp.setDataSource(getActivity().getApplicationContext(), uri);//setting new data source
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getActivity().getApplicationContext(), "ERROR", Toast.LENGTH_SHORT).show();///PROBLEM:MOVING HERE AFTER CLICKING NEXT BUTTON
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();//PROBLEM: NOT PLAYING
Toast.makeText(getActivity().getApplicationContext(), ""+uri, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
play.setImageResource(R.drawable.play);
}
}
fragment_movies.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:background="#CECEF6"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="398dp"
android:layout_weight="1.44"
android:scaleType="fitXY"
android:choiceMode="none"
android:scrollingCache="false" >
</ListView>
<VideoView
android:id="@+id/video1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="63dp" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="bottom"
android:scaleType="fitXY"
android:padding="2dp" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:scaleType="fitXY" >
<ImageButton
android:id="@+id/prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:padding="8dp"
android:src="@drawable/prev" />
<TextView
android:layout_width="15dp"
android:layout_height="wrap_content"
android:scaleType="fitXY"
/>
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:padding="8dp"
android:src="@drawable/play" />
<TextView
android:layout_width="15dp"
android:layout_height="wrap_content"
android:scaleType="fitXY"
/>
<ImageButton
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:padding="8dp"
android:src="@drawable/next" />
</TableRow>
</TableLayout>
</FrameLayout>
</LinearLayout>
更新: 以下是logcat
10-14 18:57:45.311: W/System.err(7545): java.lang.NullPointerException
10-14 18:57:45.311: W/System.err(7545): at info.androidhive.tabsswipe.VideoFragment$1.onItemClick(VideoFragment.java:150)
10-14 18:57:45.312: W/System.err(7545): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
10-14 18:57:45.312: W/System.err(7545): at android.widget.AbsListView.performItemClick(AbsListView.java:1128)
10-14 18:57:45.312: W/System.err(7545): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2815)
10-14 18:57:45.312: W/System.err(7545): at android.widget.AbsListView$1.run(AbsListView.java:3574)
10-14 18:57:45.312: W/System.err(7545): at android.os.Handler.handleCallback(Handler.java:800)
10-14 18:57:45.312: W/System.err(7545): at android.os.Handler.dispatchMessage(Handler.java:100)
10-14 18:57:45.312: W/System.err(7545): at android.os.Looper.loop(Looper.java:194)
10-14 18:57:45.312: W/System.err(7545): at android.app.ActivityThread.main(ActivityThread.java:5371)
10-14 18:57:45.313: W/System.err(7545): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 18:57:45.313: W/System.err(7545): at java.lang.reflect.Method.invoke(Method.java:525)
10-14 18:57:45.313: W/System.err(7545): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
10-14 18:57:45.313: W/System.err(7545): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-14 18:57:45.313: W/System.err(7545): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
更改video.setVisibility(view.VISIBLE)
;到video.setVisibility(View.VISIBLE);