我的Android视频播放器没有错误,但启动时显示空白屏幕

时间:2015-12-29 23:37:07

标签: android android-studio

这已经问了一段时间了,我不知道如何解决它 我的代码绝对没有任何错误,但是无论何时我测试它都显示我是一个空白的白色屏幕任何想法为什么以及如何阻止它因为我有一个截止日期。

这是我的MainActivity.java

package com.android.beez.play;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.VideoView;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;

public class MainActivity extends ListActivity {

ArrayList<String> videoPath= new ArrayList<String>();
String ext=".3gp";
String ext2=".mp4";
final Context context=this;

public class GenericExtFilter implements FilenameFilter {

    private String ext;

    public GenericExtFilter(String ext) {
        this.ext = ext;
    }

    public boolean accept(File dir, String name) {
        return (name.endsWith(ext));
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    File files = new File(Environment.getExternalStorageDirectory()+ "/");
    Log.i("files.........",files.toString());
    GenericExtFilter filter = new GenericExtFilter(ext);
    String[] list = files.list(filter);
    GenericExtFilter filter2 = new GenericExtFilter(ext2);
    String[] list2 = files.list(filter2);

    for(int i=0;i<list.length | i<list2.length;++i)
    {
        videoPath.add(i,list[i].toString());
        videoPath.add(i+1,list2[i].toString());
    }

    setListAdapter(new ArrayAdapter<String>(this,            R.layout.activity_main,videoPath));

    ListView listView = getListView();
    listView.setTextFilterEnabled(true);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,int
        position, long id) {             
            String path="/mnt/sdcard/";
            path=path+videoPath.get(position);
            Log.i("path of video file......",path);
            video(path);
        }
    });
}

public void video(String path)
{
    Intent i=new Intent(context,PlayVideo.class);
    i.putExtra("path",path);
    startActivity(i);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
}

和我的activity_main

   <?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"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:background="#000000"
   app:layout_behavior="@string/appbar_scrolling_view_behavior"
   tools:context="com.android.beez.play.MainActivity"
   tools:showIn="@layout/activity_main">

  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="20sp"
    android:background="#0432bc">


   </TextView>
   </RelativeLayout>

和我的video_layout

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#000000">

    <Button
    android:id="@+id/back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="20dp"
    android:text="Back to List"
    android:background="#0432bc"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"/>
    <VideoView
    android:id="@+id/video"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_marginTop="30dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="30dp"
    android:layout_marginBottom="10dp"
    />

</LinearLayout>

和我的PlayVideo.java

package com.android.beez.play;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

public class PlayVideo extends Activity {
Button back;
final Context context=this;
VideoView v;
String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.video_layout);
    back=(Button)findViewById(R.id.back);
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    v=(VideoView)findViewById(R.id.video);
    Intent i=getIntent();
    path=i.getStringExtra("path");
    Log.i("path of video file......", path);
    v.setVideoPath(path);

    v.setMediaController(new MediaController(context));
    v.requestFocus();

    v.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer arg0) {
            v.start();
        }
    });

    v.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        public void onCompletion(MediaPlayer mp) {
            mp.reset();
            v.setVideoPath(path);
            v.start();
        }
        });
        }
        }

1 个答案:

答案 0 :(得分:0)

尝试使用setContentView(R.layout.activity_main) MainActivity方法拨打onCreate