请求在片段中使用相机权限

时间:2017-06-14 12:32:06

标签: android android-fragments android-camera

我正在编写应用程序,并且由于灵活性,我决定在其设计中使用片段,但是当我运行应用程序时,我得到了nullobjectreference。问题是片段没有等待用户授予权限,因为它是异步运行的,因此当它创建相机实例时,由于缺少权限而导致它变为空,即使我先检查它们。这是onCreateView的代码:

NULLIF

当我创建相机实例时:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    if(checkCameraHardware(this.getActivity())){
        checkPermissions();
    }else {
        try {
            this.finalize();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

    LinearLayout buttonLayout = (LinearLayout)setButton();


    mFrameLayout = new FrameLayout(this.getActivity());
    FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    frameParams.gravity = Gravity.BOTTOM;
    mFrameLayout.setLayoutParams(frameParams);

    mFrameLayout.addView(mPreview);
    mFrameLayout.addView(buttonLayout);


    return mFrameLayout;
}

我知道我无法暂停线程以获取权限请求,但我该怎么办?

1 个答案:

答案 0 :(得分:-1)

在运行时向您的Activity添加片段并在执行此操作前请求权限