Activity 在 onActivityResult 方法上变为空

Posted

技术标签:

【中文标题】Activity 在 onActivityResult 方法上变为空【英文标题】:Activity becomes null on onActivityResult method 【发布时间】:2020-02-26 13:22:18 【问题描述】:

当用户按下按钮时,我启动相机,用户捕获图像,然后按确定,应用程序在棒棒糖设备上崩溃。

它在其他设备上运行良好。

当我检查时,当用户在捕获图像后返回时,所有变量和活动中的所有内容都变为空。我不知道为什么 2 个棒棒糖低端设备使所有变量都为空。

这是我的代码。

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

【问题讨论】:

添加你的 logcat 错误 @kam1234 : 添加 logcat Logcat 没有说活动为空,但它说结果包含空。你能发布 onActivityResult 方法吗? @SebastianPakieła : 添加了 onActivityResult 方法,但是当我在调试点停止程序并检查其他变量时,在此活动中它们都为空 【参考方案1】:

尝试添加这个

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


并在 requestPermission 方法中

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

【讨论】:

以上是关于Activity 在 onActivityResult 方法上变为空的主要内容,如果未能解决你的问题,请参考以下文章

如何在view中取得activity对象

android中 activity1中启动了activity2, activity2中启动了activity3,怎么把activity3中的值直接返回给

Android studio怎么创建一个activity文件

android activity之间怎么跳转?

Android studio怎么创建一个activity文件

如何在activity中获取fragment的控件,然后修改控件的内容,