【问题标题】:null pointer exceptions when capture image捕获图像时出现空指针异常
【发布时间】:2014-09-11 18:38:29
【问题描述】:

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

点击按钮方法

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

        }
    }
}

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

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

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

【问题讨论】:

  • 哪个类是“RegisterMember”?它正在尝试交付您的作物成果,但这项活动已经完成了吗?
  • 尝试突出显示导致空引用的行在您的代码中的位置。 RegisterMember.java:99
  • @Martin。我编辑了代码以显示第 99 行
  • picViewnull。您需要确定原因。
  • @KalelWade 第 99 行 [picView.setImageBitmap(thePic);] 当我想将图像的内容设置到图像视图上时。那就是我有空指针异常的地方

标签: android nullpointerexception android-imageview


【解决方案1】:

最可能的原因是因为当前Activity中没有id为R.id.imageViewMemberRegistration的ImageView。如果您在排查问题时需要帮助,请发布您的活动的 XML 文件。 功劳归于 Code-Apprentice

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多