需要android.app.Fragment

时间:2016-06-06 17:28:21

标签: java android android-fragments

我正在使用android.support.v4.app.Fragment。但是,它要求我使用android.app.Fragment我不能。我该如何解决以下问题:

import android.support.v4.app.Fragment;
public class CameraFragment extends Fragment implements View.OnClickListener, FragmentCompat.OnRequestPermissionsResultCallback {
    private void requestCameraPermission() {
       --> if (FragmentCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
           --> new ConfirmationDialog().show(getChildFragmentManager(), FRAGMENT_DIALOG);
        } else {
            FragmentCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
                    REQUEST_CAMERA_PERMISSION);
        }
    }
}
  

错误:找到:com.example.app.CameraFragment,必需android.app.Fragment

public static class ConfirmationDialog extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final android.app.Fragment parent = getParentFragment();
        return new AlertDialog.Builder(getActivity())
                .setMessage(R.string.request_permission)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        FragmentCompat.requestPermissions(parent,
                                new String[]{Manifest.permission.CAMERA},
                                REQUEST_CAMERA_PERMISSION);
                    }
                })
                .setNegativeButton(android.R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Activity activity = parent.getActivity();
                                if (activity != null) {
                                    activity.finish();
                                }
                            }
                        })
                .create();
    }
}

1 个答案:

答案 0 :(得分:1)

如果您使用的是v4支持库,则不需要FragmentCompat

    if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {

如果该方法不可用,您可能需要将build.gradle更新为新版本的v4支持库。