捕获图像时出现空指针异常

时间:2014-09-11 18:38:29

标签: android nullpointerexception android-imageview

我的应用在尝试使用相机拍照时有空指针异常。

点击按钮方法

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方法

在活动结果上,图像被发送到裁剪操作以重新调整图像大小

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
        }
    }
}

performCrop 此部分有助于执行裁剪操作,调整图像大小并返回新大小

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){

    }
}

以下是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

第99行指向

99: picView.setImageBitmap(thePic);

1 个答案:

答案 0 :(得分:0)

最可能的原因是因为当前活动中没有带有ID为R.id.imageViewMemberRegistration的ImageView。如果您需要帮助排除布局故障,请发布您的活动的XML文件。 学分归 Code-Apprentice