所以,在阅读了Android网站上有关DialogFragment
的官方教程后,我决定自己使用它。代码如下:
@Override
public void feedItemSelected(Feed item) {
if(isXHDPI == true){
FragmentManager fm = getSupportFragmentManager();
VideoFragment videoFragment = (VideoFragment) fm.findFragmentById(R.id.videoFragment);
videoFragment.displayNewFeed(item);
}else{
//TODO add DialogFragment support
VideoDialog v = new VideoDialog();
v.show(getSupportFragmentManager(), "dialog");
v.showNewVideo(item);
}
}
这段代码来自MainActivity
。它实现了一个侦听器来从另一个Fragment
获取事件并适当地响应它。对于平板电脑,它会在另一个片段中显示数据。对于小型手机,我需要显示DialogFragment
用户可以看到视频的地方。
但是,我在小型手机中获得了NullPointerException
。换句话说,DialogFragment
永远不会显示。的为什么吗
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoDialog extends DialogFragment {
MediaController controls = null;
VideoView video = null;
TextView title = null;
TextView date = null;
TextView description = null;
//------------------------------------------------------------------------------
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
//------------------------------------------------------------------------------
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState){
return inflater.inflate(
R.layout.video_fragment,
container,
false); // inflate the fragment layout
}
//------------------------------------------------------------------------------
@Override
public void onStart(){
super.onStart();
controls = new MediaController(getActivity());
title = (TextView) getActivity().findViewById(R.id.title);
date = (TextView) getActivity().findViewById(R.id.date);
description = (TextView) getActivity().findViewById(R.id.description);
video = (VideoView) getActivity().findViewById(R.id.video);
controls.setAnchorView(video);
video.setMediaController(controls);
}
//------------------------------------------------------------------------------
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()));
video.start();
Toast.makeText(getActivity(), "Video started", Toast.LENGTH_LONG).show();
}
//------------------------------------------------------------------------------
}
<小时/> StackTrace:
08-03 11:22:35.944: E/AndroidRuntime(801): FATAL EXCEPTION: main
08-03 11:22:35.944: E/AndroidRuntime(801): java.lang.NullPointerException
08-03 11:22:35.944: E/AndroidRuntime(801): at com.example.tqafragments.VideoDialog.showNewVideo(VideoDialog.java:55)
08-03 11:22:35.944: E/AndroidRuntime(801): at com.example.tqafragments.MainActivity.feedItemSelected(MainActivity.java:36)
08-03 11:22:35.944: E/AndroidRuntime(801): at com.example.tqafragments.FeedFragment$2.onItemClick(FeedFragment.java:73)
08-03 11:22:35.944: E/AndroidRuntime(801): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-03 11:22:35.944: E/AndroidRuntime(801): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
08-03 11:22:35.944: E/AndroidRuntime(801): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
08-03 11:22:35.944: E/AndroidRuntime(801): at android.widget.AbsListView$1.run(AbsListView.java:3423)
08-03 11:22:35.944: E/AndroidRuntime(801): at android.os.Handler.handleCallback(Handler.java:725)
08-03 11:22:35.944: E/AndroidRuntime(801): at android.os.Handler.dispatchMessage(Handler.java:92)
08-03 11:22:35.944: E/AndroidRuntime(801): at android.os.Looper.loop(Looper.java:137)
08-03 11:22:35.944: E/AndroidRuntime(801): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-03 11:22:35.944: E/AndroidRuntime(801): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 11:22:35.944: E/AndroidRuntime(801): at java.lang.reflect.Method.invoke(Method.java:511)
08-03 11:22:35.944: E/AndroidRuntime(801): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-03 11:22:35.944: E/AndroidRuntime(801): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-03 11:22:35.944: E/AndroidRuntime(801): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
DialogFragment创建类似
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.video_fragment, null);
builder.setView(view);
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);
return builder.create();
}