Reputation: 51
I tried to view the camera in my FrameLayout fragment. However, i got java.lang.NullPointerException. This is the fragment code:
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);
This is the layout for the fragment:
<?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>
This is the CameraPreview class:
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);
}
I am pretty sure there is no addView() in Fragment. How I should fix this. Thanks
Upvotes: 0
Views: 4344
Reputation: 51
Solved:
on the ScanFragment class, I called the correct layout. addView() method was not the culprit here.
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;
}
And I forgot to add Camera Permission in android manifest.
<uses-permission android:name="android.permission.CAMERA"/>
Upvotes: 4