为什么我得到一个NullPointer?

时间:2011-07-03 12:51:35

标签: android video

我试图创建一个视频播放器,从sdcard中的路径获取视频。但是我得到了一个nullpointer!你能帮帮我吗? 这是代码:

class VideoFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".wmv"));
    }
}
public class video extends ListActivity {

    private static final String MEDIA_PATH = new String("/sdcard/videodata/");
    private List<String> songs = new ArrayList<String>();
    ArrayAdapter<String> videoList ;
    File home = new File(MEDIA_PATH);
private VideoView mVideoView;

    @Override
    public void onCreate(Bundle icicle) {


           try {
                super.onCreate(icicle);
                setContentView(R.layout.main3);
                updateVideoList();
            } catch (NullPointerException e) {
                Log.v(getString(R.string.app_name), e.getMessage());
            }


    }

    private void updateVideoList() {
        // TODO Auto-generated method stub 
        if (home.listFiles( new VideoFilter()).length > 0) {
            for (File file : home.listFiles( new VideoFilter())) {
                songs.add(file.getName());
            }
    videoList = new ArrayAdapter<String>(this,R.layout.video_item,songs);

            setListAdapter(videoList);
        }
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {



        mVideoView.setVideoPath(MEDIA_PATH + songs.get(position));
        mVideoView.setMediaController(new MediaController(this));
        mVideoView.requestFocus();
    }

}

@martin如果我这样做

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        setContentView(R.layout.main2);
        mVideoView = (VideoView) findViewById(R.id.VideoView);

        mVideoView.setVideoPath(MEDIA_PATH + songs.get(position));
        mVideoView.setMediaController(new MediaController(this));
        mVideoView.requestFocus();
    }

然后我得到07-03 15:55:55.190: ERROR/AndroidRuntime(4090): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

编辑:

public class mainscreen extends Activity {

    Bundle extras = getIntent().getExtras();
    final int VIDEOS = extras.getInt("VIDEO");  
    private VideoView mVideoView;

   String k= new Integer(VIDEOS).toString();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main2);

        mVideoView = (VideoView) findViewById(R.id.VideoView);
        mVideoView = (VideoView) findViewById(R.id.video);
        mVideoView.setVideoPath(k);
        mVideoView.setMediaController(new MediaController(this));
        mVideoView.requestFocus();

        }

这就是我从第一个活动传递路径字符串的方式,第二个但是它不起作用.............

1 个答案:

答案 0 :(得分:3)

您需要先获取视频观看次数:

mVideoView = (VideoView) findViewById(R.id.myvideoview);

修改

您的内容必须包含一个ListView,其id属性为'android.R.id.list'。就这么简单。在main2.xml中,您需要一个ID为list的ListView。

<强> EDIT2

因此,您希望在播放视频时摆脱ListView。我建议确实为此开始一个新的活动。 ListActivity不允许没有ListView的内容。