Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change activity without input data?

I am trying to use intent to pass input data to another activity. it works well by the way, but when I try to change the activity without any input data, application shuts down. How can I change the activity without input data and image file?

Here is the upload activity code :

imageView = (ImageView) findViewById(R.id.imageview);
    Button uploading = (Button) findViewById(R.id.upload_upload);
    uploading.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
            float scale = (float) (1024/(float)bitmap.getWidth());
            int image_w = (int) (bitmap.getWidth() * scale);
            int image_h = (int) (bitmap.getHeight() * scale);
            Bitmap resize = Bitmap.createScaledBitmap(bitmap, image_w, image_h, true);
            resize.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            byte[] byteArray = stream.toByteArray();

            Intent intent = new Intent(upload.this,MainActivity.class);

            intent.putExtra("integer", 300);
            intent.putExtra("double", 3.141592);
            intent.putExtra("image", byteArray);


            EditText input_title = (EditText) findViewById(R.id.upload_title);
            String temp1 = input_title.getText().toString();
            intent.putExtra("temp1",temp1);

            EditText input_author = (EditText) findViewById(R.id.upload_author);
            String temp2 = input_author.getText().toString();
            intent.putExtra("temp2",temp2);
            
            startActivity(intent);
        }
    });

MainActivity code :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ImageView imageView = (ImageView) findViewById(R.id.main_image);

    Bundle extras = getIntent().getExtras();
    byte[] byteArray = getIntent().getByteArrayExtra("image");
    Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

    imageView.setImageBitmap(bitmap);

    text_title = (TextView) findViewById(R.id.title); 

    Intent receiver = getIntent();

    String title_temp = receiver.getStringExtra("temp1");
    text_title.setText(title_temp);

    text_author = (TextView)  findViewById(R.id.author);

    String author_temp = receiver.getStringExtra("temp2");
    text_author.setText(author_temp);

>Solution :

You didn’t add any crash log, but by looking at your code I see you don’t verify the Intent extras is not null. This may lead to a NullPointerException and could definitely be the reason for the crash.

Add a null check before using the Intent:

Bundle extras = getIntent().getExtra();
if (extras != null) {
    ImageView imageView = (ImageView) findViewById(R.id.main_image);

    byte[] byteArray = receiver.getByteArrayExtra("image");
    Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

    imageView.setImageBitmap(bitmap);

    text_title = (TextView) findViewById(R.id.title); 


    String title_temp = receiver.getStringExtra("temp1");
    text_title.setText(title_temp);

    text_author = (TextView)  findViewById(R.id.author);

    String author_temp = receiver.getStringExtra("temp2");
    text_author.setText(author_temp);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading