NullPointerException但不明白为什么

时间:2013-08-04 06:13:53

标签: java android nullpointerexception android-dialogfragment

我有一个DialogFragment,它应该显示VideoView和一些TextViews。但是,VideoView与其他控件一样为空,即使布局已经膨胀。

以下是我检索指向各种控件的指针的代码:

@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);
}  

但是,调试员说不然:
enter image description here

为什么他们是空的? 我在创建视图中对布局进行了充气,​​定义了它们。

StackTrace:

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)  

ShowNewVideo:

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();
        }

1 个答案:

答案 0 :(得分:0)

你需要做一些改变

把你的代码

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);

从onViewCreated()到onCreateView()然后在onViewCreated()方法上绑定数据。