Android onActivityResult方法上的活动变为null
当用户按下按钮,我启动相机,用户捕获图像,然后按下ok,应用程序在棒棒糖设备上崩溃 它可以在其他设备上正常工作 当我检查时,当用户在捕获图像后返回时,活动中的所有变量和所有内容都将变为null。我不知道为什么2个棒棒糖低端设备使所有变量为空 这是我的密码Android onActivityResult方法上的活动变为null,android,android-layout,android-intent,android-activity,Android,Android Layout,Android Intent,Android Activity,当用户按下按钮,我启动相机,用户捕获图像,然后按下ok,应用程序在棒棒糖设备上崩溃 它可以在其他设备上正常工作 当我检查时,当用户在捕获图像后返回时,活动中的所有变量和所有内容都将变为null。我不知道为什么2个棒棒糖低端设备使所有变量为空 这是我的密码 public void launchCamera(){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (getPermissions(Manif
public void launchCamera(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (getPermissions(Manifest.permission.CAMERA, CAMERA_PERMISSION_CODE)) {
openCamera();
}
}else{
openCamera();
}
}
public void openCamera(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
if (photoFile != null) {
String authorities = getApplicationContext().getPackageName() + ".fileprovider";
photoURI = FileProvider.getUriForFile(this,
authorities,
photoFile);
try{
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, 1);
}catch (Exception ex){
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
Logcat
java.lang.RuntimeException: Unable to resume activity {com.app.myapp/com.app.myapp}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.app.myapp}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.app.myapp}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
编辑
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
switch(requestCode) {
case CAMERA_RESULT:
if (responseCode == RESULT_OK) {
onProfilePicAddedFromCamera(photoURI, mCurrentPhotoPath);
}
break;
case GALLERY_RESULT:
if (responseCode == RESULT_OK) {
Uri selectedImage = intent.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
onProfilePicAddedFromGallery(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
mTwitterAuthClient.onActivityResult(requestCode, responseCode, intent);
callbackManager.onActivityResult(requestCode, responseCode, intent);
}
试着加上这个
public void launchCamera(){
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.CAMERA)) {
//Show permission dialog
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.CAMERA}, code);
}
}
else
openCamera();
}
和在请求权限方法中
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openCamera();
} else {
Log.e("", "Permission denied");
}
}
}
}
添加你的日志error@kam1234:added logcatLogcat不表示活动为null,但表示结果包含null。你能发布onActivityResult方法吗?@SebastianPakieła:添加了onActivityResult方法,但当我在调试点停止程序,并检查其他变量时,它们在该活动的此时都为空