我正在使用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();
}
}
答案 0 :(得分:1)
如果您使用的是v4支持库,则不需要FragmentCompat
:
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
如果该方法不可用,您可能需要将build.gradle更新为新版本的v4支持库。