NullPointerException:显示列表中的视频
您好,我正在尝试显示列表中的视频,但我不断收到空指针异常,有什么想法吗?
这是我的代码:
public class HallList extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
R.array.lecturehalls ,R.layout.list_item));
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0)
{ video(position);
}
else if(position == 1)
{
video(position);
}
}
});
}
private void video(int position){
MediaController mediaController = new MediaController(this);
VideoView videoView = (VideoView) findViewById(R.id.video);
if (position == 0){
//example path
String path0="http://www.pocketjourney.com/downloads/pj/video/famous.3gp";
mediaController.setMediaPlayer(videoView);
videoView.setVideoPath(path0);
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.start();
mediaController.show(); }
else if( ........ ){...}
}
}
我不确定我的做法是否正确。如果有更简单的方法,请帮助我。我看到了一个显示列表中的 Web 链接的教程。我想要以相同的方式创建它,但似乎不知道如何使用视频链接列表完成它? 这是教程: http://mobile.tutsplus.com/tutorials/android/android -列表视图/ 谢谢:)
Hi I'm trying to display a video from a list and I keep getting the null pointer exception any ideas why?
This is my code:
public class HallList extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
R.array.lecturehalls ,R.layout.list_item));
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0)
{ video(position);
}
else if(position == 1)
{
video(position);
}
}
});
}
private void video(int position){
MediaController mediaController = new MediaController(this);
VideoView videoView = (VideoView) findViewById(R.id.video);
if (position == 0){
//example path
String path0="http://www.pocketjourney.com/downloads/pj/video/famous.3gp";
mediaController.setMediaPlayer(videoView);
videoView.setVideoPath(path0);
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.start();
mediaController.show(); }
else if( ........ ){...}
}
}
I'm not sure of my way of doing it right..If there's an easier way to do it could you please help me with it..I saw a tutorial displaying web links from a list..I want to create it the same way but don't seem to get how its done with list of video links?
this is the tutorial: http://mobile.tutsplus.com/tutorials/android/android-listview/
Thanx :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在哪个 xml 文件中定义 VideoView?如果没有
setContentView()
你正在定义你的 videoView(我猜没有看到你的错误日志)我认为这一行会导致 nullPointerException,
你必须在你的 xml 文件中定义 VIideoView 并将该 xml 文件设置为
setContentView(main.xml)
并将此行放在 Activity 的 onCreate() 中的 setContentView() 之后。
In which xml file you define your VideoView? Without
setContentView()
your are defining your videoView(Without seeing your error log I am guessing) I think this line cause nullPointerException,
You have to define VIideoView in your xml file and set that xml file as a
setContentView(main.xml)
also put this line after setContentView() in onCreate() of your activity..
在调试器中运行代码,然后查看堆栈跟踪以查看抛出异常的位置。
Run your code in a debugger, then look at the stack trace to see where the exception was thrown.
您还没有在代码中设置setContentView。并且您在 OnCreate 方法中获取 getListView 这将导致空指针异常。
You have not set the setContentView in the code. and you are getting the getListView in the OnCreate method which will result in to null pointer exception.