我试图在FrameLayout片段中查看相机。但是,我得到了java.lang.NullPointerException。这是片段代码:
public class ScanFragment extends Fragment {
public ScanFragment(){}
private Camera mCamera;
private CameraPreview mPreview;
private Handler autoFocusHandler;
private static final String TAG_PID = "pid";
TextView scanText;
ImageScanner scanner;
private boolean barcodeScanned = false;
private boolean previewing = true;
static {
System.loadLibrary("iconv");
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
autoFocusHandler = new Handler();
mCamera = getCameraInstance();
/* Instance barcode scanner */
scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);
mPreview = new CameraPreview(this.getActivity(), mCamera, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout) rootView.findViewById(R.id.cameraPreview);
preview.addView(mPreview);
scanText = (TextView) rootView.findViewById(R.id.scanText);
return rootView;
}
// create object CameraPriew
mPreview = new CameraPreview(this.getActivity(), mCamera, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout) rootView.findViewById(R.id.cameraPreview);
// View Camera in FrameLayout of the fragment
preview.addView(mPreview);
这是片段的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/cameraPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<TextView
android:id="@+id/scanText"
android:text=""
android:layout_height="wrap_content"
android:layout_width="match_parent">
</TextView>
</LinearLayout>
这是CameraPreview类:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private PreviewCallback previewCallback;
private AutoFocusCallback autoFocusCallback;
public CameraPreview(Context scanFragment, Camera camera,
PreviewCallback previewCb,
AutoFocusCallback autoFocusCb) {
super(scanFragment);
mCamera = camera;
previewCallback = previewCb;
autoFocusCallback = autoFocusCb;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
我很确定Fragment中没有addView()。我该如何解决。谢谢
最佳答案
解决了:
在ScanFragment类上,我调用了正确的布局。 addView()方法不是这里的罪魁祸首。
public class ScanFragment extends Fragment {
public ScanFragment(){}
private Camera mCamera;
private CameraPreview mPreview;
private Handler autoFocusHandler;
private static final String TAG_PID = "pid";
TextView scanText;
ImageScanner scanner;
private boolean barcodeScanned = false;
private boolean previewing = true;
FrameLayout preview;
static {
System.loadLibrary("iconv");
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// previously fragment_home
View rootView = inflater.inflate(R.layout.fragment_scan, container, false);
autoFocusHandler = new Handler();
mCamera = getCameraInstance();
mPreview = new CameraPreview(this.getActivity(), mCamera, previewCb, autoFocusCB);
preview = (FrameLayout) rootView.findViewById(R.id.cameraPreview);
preview.addView(mPreview);
scanText = (TextView) rootView.findViewById(R.id.scanText);
return rootView;
}
而且我忘了在android清单中添加Camera Permission。
<uses-permission android:name="android.permission.CAMERA"/>