我正在调用getHolder时在片段中获取空指针异常。 这里是片段的代码
public class Camera_Fragment extends Fragment implements IVideoPlayer, View.OnClickListener {
Button up, dowm, right, left;
SurfaceView CamView;
private String VideoUrl = "http://192.168.0.4:81/videostream.cgi?user=admin&password=admin";
private SurfaceHolder Holder;
private Surface surface = null;
private LibVLC libVLC;
private int mVideoHeight;
private int mVideoWidth;
private int mVideoVisibleHeight;
private int mVideoVisibleWidth;
private int mSarNum;
private int mSarDen;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.cam1, container, false);
CamView = (SurfaceView) view.findViewById(R.id.Cam1_CamVIew);
up = (Button) view.findViewById(R.id.Cam_up);
dowm = (Button) view.findViewById(R.id.Cam_down);
right = (Button) view.findViewById(R.id.Cam_right);
left = (Button) view.findViewById(R.id.Cam_left);
up.setOnClickListener(Camera_Fragment.this);
dowm.setOnClickListener(Camera_Fragment.this);
right.setOnClickListener(Camera_Fragment.this);
left.setOnClickListener(Camera_Fragment.this);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cam_Play();
}
private void Cam_Play() {
Holder = CamView.getHolder();
try {
libVLC = new LibVLC();
libVLC.setVout(LibVLC.VOUT_ANDROID_SURFACE);
libVLC.setHardwareAcceleration(LibVLC.HW_ACCELERATION_AUTOMATIC);
libVLC.init(getActivity().getApplicationContext());
} catch (LibVlcException e) {
e.printStackTrace();
}
surface = Holder.getSurface();
libVLC.attachSurface(surface, this);
libVLC.playMRL(VideoUrl);
}
上面的代码在一个Activity中运行完美..但是在片段中它不是..我仍然是Android开发中的一个大问题。我第一次使用片段。任何解决方案或建议。 这里是logcat
11-17 01:03:52.301 5860-5860/com.fyp.tirz.wifisurveillance E/AndroidRuntime: FATAL EXCEPTION: main
11-17 01:03:52.301 5860-5860/com.fyp.tirz.wifisurveillance E/AndroidRuntime: Process: com.fyp.tirz.wifisurveillance, PID: 5860
11-17 01:03:52.301 5860-5860/com.fyp.tirz.wifisurveillance E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.SurfaceHolder android.view.SurfaceView.getHolder()' on a null object reference
11-17 01:03:52.301 5860-5860/com.fyp.tirz.wifisurveillance E/AndroidRuntime: at Fragments.Camera_Fragment.onCreate(Camera_Fragment.java:62)
答案 0 :(得分:0)
删除它。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cam_Play();
}
在此处添加Cam_Play。
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.cam1, container, false);
CamView = (SurfaceView) view.findViewById(R.id.Cam1_CamVIew);
up = (Button) view.findViewById(R.id.Cam_up);
dowm = (Button) view.findViewById(R.id.Cam_down);
right = (Button) view.findViewById(R.id.Cam_right);
left = (Button) view.findViewById(R.id.Cam_left);
up.setOnClickListener(Camera_Fragment.this);
dowm.setOnClickListener(Camera_Fragment.this);
right.setOnClickListener(Camera_Fragment.this);
left.setOnClickListener(Camera_Fragment.this);
Cam_Play(); //< ----------------( ADD IT HERE )
return view;
}