我有一个DialogFragment
应该显示 aVideoView
和一些TextViews
. 但是,VideoView
与其他控件一起为 null,即使布局已膨胀。
这是我检索指向各种控件的指针的代码:
@Override
public void onViewCreated (View view, Bundle savedInstanceState) {
controls = new MediaController(getActivity());
title = (TextView) view.findViewById(R.id.title);
date = (TextView) view.findViewById(R.id.date);
description = (TextView) view.findViewById(R.id.description);
video = (VideoView) view.findViewById(R.id.video);
controls.setAnchorView(video);
video.setMediaController(controls);
}
但是,调试器另有说明:
为什么它们是空的?我在定义它们的创建视图中夸大了布局。
堆栈跟踪:
08-04 06:19:07.804: E/AndroidRuntime(1758): FATAL EXCEPTION: main
08-04 06:19:07.804: E/AndroidRuntime(1758): java.lang.NullPointerException
08-04 06:19:07.804: E/AndroidRuntime(1758): at com.example.tqafragments.VideoDialog.showNewVideo(VideoDialog.java:61)
08-04 06:19:07.804: E/AndroidRuntime(1758): at com.example.tqafragments.MainActivity.feedItemSelected(MainActivity.java:36)
08-04 06:19:07.804: E/AndroidRuntime(1758): at com.example.tqafragments.FeedFragment$2.onItemClick(FeedFragment.java:73)
08-04 06:19:07.804: E/AndroidRuntime(1758): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-04 06:19:07.804: E/AndroidRuntime(1758): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
08-04 06:19:07.804: E/AndroidRuntime(1758): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
08-04 06:19:07.804: E/AndroidRuntime(1758): at android.widget.AbsListView$1.run(AbsListView.java:3423)
08-04 06:19:07.804: E/AndroidRuntime(1758): at android.os.Handler.handleCallback(Handler.java:725)
08-04 06:19:07.804: E/AndroidRuntime(1758): at android.os.Handler.dispatchMessage(Handler.java:92)
08-04 06:19:07.804: E/AndroidRuntime(1758): at android.os.Looper.loop(Looper.java:137)
08-04 06:19:07.804: E/AndroidRuntime(1758): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-04 06:19:07.804: E/AndroidRuntime(1758): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 06:19:07.804: E/AndroidRuntime(1758): at java.lang.reflect.Method.invoke(Method.java:511)
08-04 06:19:07.804: E/AndroidRuntime(1758): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-04 06:19:07.804: E/AndroidRuntime(1758): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-04 06:19:07.804: E/AndroidRuntime(1758): at dalvik.system.NativeStart.main(Native Method)
显示新视频:
public void showNewVideo(Feed f){
title.setText(f.getTitle());
date.setText(f.getDate());
description.setText(Html.fromHtml(f.getDescription()));
video.setVideoURI(Uri.parse(f.getVideoURL())); // Line 61
video.start();
}