Android onActivityResult错误

时间:2015-09-27 08:36:47

标签: android

我编写了以下代码onActivityResult方法来调用CropImage.class,它将提供裁剪后的图像,但是我在从Crop Activity返回到Add_Customer Activity时遇到错误

使用以下方法传递3来调用CropImage活动:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Toast.makeText(getApplicationContext(), "Called Succesfully..." + requestCode, Toast.LENGTH_SHORT).show();
        if (requestCode == 1)
        {
            Intent intent = new Intent(Add_Customer.this,CropImage.class);
            startActivityForResult(intent, 3);
        }

        if (requestCode ==3)
        {
            Toast.makeText(getApplicationContext(),"Photo Added Succesfully...", Toast.LENGTH_SHORT).show();
            File f = new File(Environment.DIRECTORY_PICTURES);
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    break;
                }
            }
            BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

            Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath(),
                    bitmapOptions);

            viewImage.setImageBitmap(bmp);
        }

    }

CropImage活动onCreate:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop_image);

final CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
final ImageView croppedImageView = (ImageView)findViewById(R.id.croppedImageView);
        // Set image for cropping

        File f = new File(Environment.getExternalStorageDirectory().toString());
        for (File temp : f.listFiles()) 
        {
          if (temp.getName().equals("temp.jpg")) 
          {
            f = temp;
            break;
          }
        }
        try
        {

        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

        bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
        bitmapOptions);

        cropImageView.setImageBitmap(bitmap);



        //deleting image captured by camera
        f.delete();


        } catch (Exception e) {
        e.printStackTrace();
        }




        Button cropButton = (Button)findViewById(R.id.crop_button);
        cropButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
        // Get cropped image, and show result.
        croppedImageView.setImageBitmap(cropImageView.getCroppedBitmap());

        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        Bitmap crop = cropImageView.getCroppedBitmap();

        OutputStream outFile = null;
        File file = new File(Environment.DIRECTORY_PICTURES, "temp.jpg");
        try {
        outFile = new FileOutputStream(file);
        crop.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
        outFile.flush();
        outFile.close();

        } catch (Exception e) {
        e.printStackTrace();
        }


        Intent intentMessage = new Intent();

        // put the message in Intent
        intentMessage.putExtra("MESSAGE", "hello");
        intentMessage.putExtra("image", crop);
        setResult(3, intentMessage);
        // finish The activity
        finish();
        }
        });
        }

错误:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=3, data=Intent { (has extras) }} to activity {loginscreen.example.com.girviapp/loginscreen.example.com.girviapp.Add_Customer}: java.lang.NullPointerException: Attempt to get length of null array
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3539)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
            at android.app.ActivityThread.access$1300(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to get length of null array
            at loginscreen.example.com.girviapp.Add_Customer.onActivityResult(Add_Customer.java:196)
            at android.app.Activity.dispatchActivityResult(Activity.java:6135)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3535)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
            at android.app.ActivityThread.access$1300(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

2 个答案:

答案 0 :(得分:0)

你的回调意图没有传递正确的resultCode,而是因为它崩溃了。

在CropImage活动的onClick()中更改您的代码:

Intent intentMessage = new Intent();

    // put the message in Intent
    intentMessage.putExtra("MESSAGE", "hello");
    intentMessage.putExtra("image", crop);
    setResult(RESULT_OK, intentMessage);
    // finish The activity
    finish();

答案 1 :(得分:0)

我在onActivityResult方法中使用getIntent.getByteArrayExtra(“image”)而不是data.getByteArrayExtra(“image”)犯了愚蠢的错误