-3

I am working on camera application, where it take a picture and pass it to a another activity as a bitmap.

But the problem is that when I press on "onKeyDown (BACK)" button I get java.lang.NullPointerException error, and the logcat is pointing to this code:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        Intent mIntent = new Intent(this, SecondActivity.class);
       >>> Bitmap photo = (Bitmap) data.getExtras().get("data"); <<<
        mIntent.putExtra("data", photo);
        startActivityForResult(mIntent, 0);
}

How to solve this problem? Everytime when I press on back button and I want to go to my main activity, app has been stopped and got following exception:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference

 public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK);
        Intent intent = new Intent(this, FirstActivity.class);
        return super.onKeyDown(keyCode, event);
    }
Bhavik Kamdar
  • 301
  • 1
  • 7
  • 15
Tin_Ram
  • 99
  • 1
  • 11
  • 1
    You need to ask if(resultCode == >Insert-your-declared-Number-here<) otherwise there is no result. Your if in onKeyDown is wrong aswell. You put a semicolon on the end of the line so your IF statement does **NOTHING**. Read this for further informations http://developer.android.com/reference/android/app/Activity.html#StartingActivities – B. Kemmer May 07 '15 at 10:07
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Simon May 07 '15 at 11:12

3 Answers3

1

The thing here is that when you startActivityForResult for taking photo, you should put extra in intent like this:

                File file = new File(Environment
                                .getExternalStorageDirectory(), "temp.jpg");
                outputImageUri = Uri.fromFile(file);
                Intent intent = new Intent(
                                MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, outputImageUri);
                        startActivityForResult(intent, TAKE_PICTURE);

Then you should check if data is null

               // Check if the result includes a thumbnail Bitmap
                if (data != null) {
                    if (data.hasExtra("data")) {
                        Bitmap thumbnail = data.getParcelableExtra("data");
                        imageView.setImageBitmap(thumbnail);
                    }
                } else {
                    // If there is no thumbnail image data, the image
                    // will have been stored in the target output URI.
                    Bitmap bitmap = BitmapFactory.decodeFile(
                            outputImageUri.getPath(), factoryOptions);
                    imageView.setImageBitmap(bitmap);
                }

Because data can be null

Nik Myers
  • 1,843
  • 19
  • 28
1

The onKeyDown is wrong:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == event.KEYCODE_BACK); // Semicolon here, ends the "if"
    Intent intent = new Intent(this, FirstActivity.class);
    // The intent has no extra "data"
    // The intent is never used to set the result
    return super.onKeyDown(keyCode, event); // Overwrites the result
}

It should be something like this:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == event.KEYCODE_BACK) {
        Intent intent = new Intent(this, FirstActivity.class);
        intent.putExtra("data", theBitmap);
        setResult(RESULT_OK, intent);
        // Avoid "super.onKeyDown" so it won't overwrite the result
        finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Note: for better user experience, this should actually be done in onKeyUp

You should also check the resultCode in onActivityResult

StenSoft
  • 9,369
  • 25
  • 30
0

You need to check in your onActivityResult

if (resultcode == RESULT_OK ){
your code
}

OR

if(data!=null){
your code
}

Or use and operator(&&) for both

before handling the data.

Santosh Kathait
  • 1,444
  • 1
  • 11
  • 22
  • This answer doesn't deserve acception. Your provided Code isn't wrong at all, but that wasn't the cause of the Problem, which Tin_Ram has had. The cause is the semicolon at the end of his IF-Statement as @StenSoft pointed out in his answer. Firstly the questioner has to fix this failure before he can add the first IF-Statement in your answer. I would consider the second IF-Statement as bad code, since the startActivityForResult Action provides a resultcode. – B. Kemmer May 07 '15 at 11:13