gpt4 book ai didi

java - addView() fragment 中的框架布局

转载 作者:行者123 更新时间:2023-12-01 23:13:41 26 4
gpt4 key购买 nike

我尝试在 FrameLayout fragment 中查看相机。但是,我得到了 java.lang.NullPointerException。这是 fragment 代码:

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

这是 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>

这是 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 list 中添加相机权限。

<uses-permission android:name="android.permission.CAMERA"/>

关于java - addView() fragment 中的框架布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21540645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com