Attempt to Invoke virtual method'byte [ ] The error android.os.Bundle.getByteArray~ occurs.

Asked 2 years ago, Updated 2 years ago, 118 views

On Android, I write the process of receiving images from one activity to another, but when I return to my parent's activity, I get the following error, but I don't know why.
Please tell me the solution to the error.
java.lang.NullPointerException: Attempt to invite virtual method'byte[] android.os.Bundle.getByteArray(java.lang.String) 'on a null object reference

The following is the code for child activity:

public class PhotoActivity extensions AppCompatActivity{
    Bitmap bmp;
    ImageView mImageView;

    @ Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_filter);

        Bundleextras=getIntent().getExtras();
        byte[] byteArray=extras.getByteArray(CropActivity.EXTRA_CROPPED_IMAGE);

        bmp = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
        mImageView=findViewById (R.id.ImageView);
        mImageView.setImageBitmap(bmp);
    }
}

The following are parent activities:

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crop);

    Bundleextras=getIntent().getExtras();
    byte[] byteArray=extras.getByteArray(MainActivity.EXTRA_IMAGE);
    bmp = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
    mCropView=findViewById (R.id.cropImageView);
    mCropView.setImageBitmap(bmp);
}

// Some code omitted...

@ Override
public boolean onOptionsItemSelected(MenuItem){
    int id = item.getItemId();

    if(id==R.id.action_next){
        croppedBitmap=mCropView.getCropBitmap();
        ByteArrayOutputStream stream=new ByteArrayOutputStream();
        croppedBitmap.compress(Bitmap.CompressFormat.JPEG,50,stream);
        byte[] byteArray=stream.toByteArray();
        Intent = new Intent (CropActivity.this, PhotoFilterActivity.class);
        int.putExtra(EXTRA_CROPPED_IMAGE, byteArray);
        startActivity (intent);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Below are the activity elements specified for Android Manifest.

<activity android:name=".PhotoFilterActivity"
        android:label="Third Activity"
        android:parentActivityName=".CropActivity"
        android:screenOrientation="portrait">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.test.CropActivity"/>
</activity>

android android-studio

2022-09-30 19:55

1 Answers

US>By adding android:launchMode="singleTop" to each parent and child activity element covered by AndroidManifest.xml, we prevented and resolved the creation of new activities.

This post was posted as a community wiki based on @ayana Sakai's comments.


2022-09-30 19:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.