How to change activity without input data?

Viewed 9

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 :

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

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

You'll have to check if your intent have extras or not by the following check otherwise it'll throw NullpointerExxception. it will only set values if intent have extras

   if (receiver.getExtras() != null){
    
    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);
}

or you can also check specific extras by this

    if (receiver.getExtras() != null && receiver.getExtras().containsKey("temp1")) {
        String title_temp = receiver.getStringExtra("temp1");
        text_title.setText(title_temp);
    }
Related