空指针异常时捕获图像

空指针异常时捕获图像

本文介绍了空指针异常时捕获图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序都是空指针异常时,它会尝试使用相机拍摄的照片。

在按下按钮的方法

 公共无效的onClick(视图v){
    如果(v.getId()== R.id.capture_btn){
        尝试{
            //使用标准的意图拍摄图像
            意图captureIntent =新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
            //我们会处理的onActivityResult返回的数据
            startActivityForResult(captureIntent,CAMERA_CAPTURE);
        }
        赶上(ActivityNotFoundException anfe){        }
    }
}

在的onActivityResult方法

上的活动结果,图像被发送到作物操作以重新大小的图像

 保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    如果(结果code == RESULT_OK){        //用户从使用照相机拍摄图像返回
        如果(要求code == CAMERA_CAPTURE){
            //得到的URI所捕获的图像
            picUri = data.getData();            //开展作物操作
            performCrop();        }
        //用户从裁剪图像返回
        否则,如果(要求code == PIC_CROP){            //获取返回的数据
            捆绑额外= data.getExtras();
            //获取裁剪位图
            位图thePic = extras.getParcelable(数据);
            //检索到的ImageView的引用
            ImageView的picView =(ImageView的)findViewById(R.id.imageViewMemberRegistration);
            //显示返回裁剪图像
            picView.setImageBitmap(thePic); // 99号线
        }
    }
}

performCrop
这部分有助于进行作物的操作,调整图像大小,并返回新的大小

 私人无效performCrop(){    尝试{        //调用标准作物动作意图(用户设备可能不支持它)
        意图cropIntent =新意图(com.android.camera.action.CROP);
            //显示图像类型和URI
        cropIntent.setDataAndType(picUri,图像/ *);
            //设置裁剪性能
        cropIntent.putExtra(裁剪,真);
            //注明所需作物方面
        cropIntent.putExtra(aspectX,1);
        cropIntent.putExtra(aspectY,1);
            //指示输出X和Y
        cropIntent.putExtra(outputX,256);
        cropIntent.putExtra(outputY,256);
            //检索返回数据
        cropIntent.putExtra(回归数据,真正的);
            //启动活动 - 我们处理返回的的onActivityResult
        startActivityForResult(cropIntent,PIC_CROP);
    }
    赶上(ActivityNotFoundException anfe){    }
}

下面是logcat的:

  17 09-11:56:46.194:E / AndroidRuntime(1293):了java.lang.RuntimeException:由故障造成的结果交付{ResultInfo谁= NULL,请求= 2,结果= -1,数据= {意向A​​CT =内联数据(有临时演员)}}到活动{com.mobile.ppp / com.mobile.ppp.RegisterMember}:显示java.lang.NullPointerException
09-11 17:56:46.194:E / AndroidRuntime(1293):在android.app.ActivityThread.deliverResults(ActivityThread.java:3020)
09-11 17:56:46.194:E / AndroidRuntime(1293):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2546)
09-11 17:56:46.194:E / AndroidRuntime(1293):12 ...更多
09-11 17:56:46.194:E / AndroidRuntime(1293):因:显示java.lang.NullPointerException
09-11 17:56:46.194:E / AndroidRuntime(1293):在com.mobile.ppp.RegisterMember.onActivityResult(RegisterMember.java:99)
09-11 17:56:46.194:E / AndroidRuntime(1293):在android.app.Activity.dispatchActivityResult(Activity.java:4108)
09-11 17:56:46.194:E / AndroidRuntime(1293):在android.app.ActivityThread.deliverResults(ActivityThread.java:3016)
09-11 17:56:46.194:E / AndroidRuntime(1293):13 ...更多

线99点这个

  99:picView.setImageBitmap(thePic);


解决方案

最可能的原因是因为有与ID R.id.imageViewMemberRegistration当前活动没有ImageView的。如果您需要帮助解决您的布局,请张贴XML文件为您的活动。
归功于为 code-AP prentice

My app has null pointer exceptions when it tries to take a picture with the camera.

On clicked button method

public void onClick(View v) {
    if (v.getId() == R.id.capture_btn) {
        try {
            //use standard intent to capture an image
            Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //we will handle the returned data in onActivityResult
            startActivityForResult(captureIntent, CAMERA_CAPTURE);
        }
        catch(ActivityNotFoundException anfe){

        }
    }
}

On onActivityResult method

on the activity result the image is sent to the crop operation to re-size the image

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {

        //user is returning from capturing an image using the camera
        if(requestCode == CAMERA_CAPTURE){
            //get the Uri for the captured image
            picUri = data.getData();

            //carry out the crop operation
            performCrop();

        }
        //user is returning from cropping the image
        else if(requestCode == PIC_CROP){

            //get the returned data
            Bundle extras = data.getExtras();
            //get the cropped bitmap
            Bitmap thePic = extras.getParcelable("data");
            //retrieve a reference to the ImageView
            ImageView picView = (ImageView)findViewById(R.id.imageViewMemberRegistration);
            //display the returned cropped image
            picView.setImageBitmap(thePic); // Line 99
        }
    }
}

performCropThis part helps to performs a crop operation , resize the image size and return the new size

private void performCrop(){

    try {

        //call the standard crop action intent (the user device may not support it)
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
            //indicate image type and Uri
        cropIntent.setDataAndType(picUri, "image/*");
            //set crop properties
        cropIntent.putExtra("crop", "true");
            //indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
            //indicate output X and Y
        cropIntent.putExtra("outputX", 256);
        cropIntent.putExtra("outputY", 256);
            //retrieve data on return
        cropIntent.putExtra("return-data", true);
            //start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, PIC_CROP);


    }
    catch(ActivityNotFoundException anfe){

    }
}

Below is the logcat:

 09-11 17:56:46.194: E/AndroidRuntime(1293): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.mobile.ppp/com.mobile.ppp.RegisterMember}: java.lang.NullPointerException
09-11 17:56:46.194: E/AndroidRuntime(1293):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3020)
09-11 17:56:46.194: E/AndroidRuntime(1293):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2546)
09-11 17:56:46.194: E/AndroidRuntime(1293):     ... 12 more
09-11 17:56:46.194: E/AndroidRuntime(1293): Caused by: java.lang.NullPointerException
09-11 17:56:46.194: E/AndroidRuntime(1293):     at com.mobile.ppp.RegisterMember.onActivityResult(RegisterMember.java:99)
09-11 17:56:46.194: E/AndroidRuntime(1293):     at android.app.Activity.dispatchActivityResult(Activity.java:4108)
09-11 17:56:46.194: E/AndroidRuntime(1293):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3016)
09-11 17:56:46.194: E/AndroidRuntime(1293):     ... 13 more

The line 99 point to this

99: picView.setImageBitmap(thePic);
解决方案

The most likely reason is because there is no ImageView in the current Activity with id R.id.imageViewMemberRegistration. If you need help troubleshooting your layout, please post the XML file for your activity.credit goes to Code-Apprentice

这篇关于空指针异常时捕获图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:17