0

I want to set an area of a custom dialog to the selected image. The code bellow works with some re-arrangements if i'm setting the background image of the entire application. For some reason, when i move it to set an area of the custom dialog i get the following error:

ERROR:

11-03 18:19:05.216: E/AndroidRuntime(10428): FATAL EXCEPTION: main
11-03 18:19:05.216: E/AndroidRuntime(10428): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://media/external/images/media/700 }} to activity {com.example.pictures/com.example.pictures.MainActivity}: java.lang.NullPointerException
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.app.ActivityThread.access$2000(ActivityThread.java:117)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.os.Looper.loop(Looper.java:130)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.app.ActivityThread.main(ActivityThread.java:3691)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at java.lang.reflect.Method.invokeNative(Native Method)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at java.lang.reflect.Method.invoke(Method.java:507)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at dalvik.system.NativeStart.main(Native Method)
11-03 18:19:05.216: E/AndroidRuntime(10428): Caused by: java.lang.NullPointerException
11-03 18:19:05.216: E/AndroidRuntime(10428):    at com.example.pictures.MainActivity.onActivityResult(MainActivity.java:133)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.app.Activity.dispatchActivityResult(Activity.java:3950)
11-03 18:19:05.216: E/AndroidRuntime(10428):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
11-03 18:19:05.216: E/AndroidRuntime(10428):    ... 11 more

What am i missing here? I've read that this can be solved by setting the intent - but i am already doing this. Any help would be appreciated.

Thanks in advance

CODE:

public class MainActivity extends Activity {
    private static final int SELECT_PHOTO = 100;
    private Bitmap chosenBitmap; 
    final Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final DrawingView drawing = new DrawingView(this); 

        Button button = (Button) findViewById(R.id.buttonShowCustomDialog);

        // add button listener
        button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {
              final Dialog dialog = new Dialog(context);
            // custom dialog
            //dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);
            dialog.setTitle("Title...");

            // set the custom dialog components - text, image and button
            TextView text = (TextView) dialog.findViewById(R.id.text);
            text.setText("Android custom dialog example!");
            ImageView image = (ImageView) dialog.findViewById(R.id.image);
            image.setImageResource(R.drawable.ic_launcher);

            Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });


            Button captureButton = (Button) dialog.findViewById(R.id.selectImage);
            captureButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                    photoPickerIntent.setType("image/*");
                    startActivityForResult(photoPickerIntent, SELECT_PHOTO); 
                }
            });
            dialog.show();
          }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
        final Dialog dialog = new Dialog(context);

        switch(requestCode) { 
        case SELECT_PHOTO:
            if(resultCode == RESULT_OK){  
                Uri selectedImage = imageReturnedIntent.getData();
                InputStream imageStream = null;
                try {
                    imageStream = getContentResolver().openInputStream(selectedImage);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                ImageView imageview= (ImageView)dialog.findViewById(R.id.imageview);
                chosenBitmap = yourSelectedImage; 
                Drawable res = new BitmapDrawable(yourSelectedImage);
                imageview.setImageDrawable(res);
            }
        }
    }

}
BigBug
  • 6,202
  • 23
  • 87
  • 138

2 Answers2

1

When you execute that code:

InputStream imageStream = null;
try {
    imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

and you actually have an exception, the code continues and your imageStream is still null.

You should change the code to cater for that and check for null:

if (imageStream != null) {
    Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
    ImageView imageview= (ImageView)dialog.findViewById(R.id.imageview);
    chosenBitmap = yourSelectedImage; 
    Drawable res = new BitmapDrawable(yourSelectedImage);
    imageview.setImageDrawable(res);
}

or handle this situation in some other appropriate way.

This may not be the problem that's causing NPE in this case though.

Szymon
  • 42,577
  • 16
  • 96
  • 114
  • when i make this change, it becomes apparent that the line it fails at is: imageview.setImageDrawable(res); is it possible to set an imageview that is placed on a custom dialog? – BigBug Nov 04 '13 at 00:15
  • So is `imageview` null? You should do the whole setting of the image inside the dialog (where you're already building the code for the dialog). – Szymon Nov 04 '13 at 00:19
1

hichris123 asked the right question: what is line 133? That's because of this line in the stack trace:

11-03 18:19:05.216: E/AndroidRuntime(10428): at com.example.pictures.MainActivity.onActivityResult(MainActivity.java:133)

NullPointerExceptions are really pretty easy, and a little attention to detail will resolve them. You can't call a method on null, or refer to its fields.

If, for instance, line 133 was this one:

            Uri selectedImage = imageReturnedIntent.getData();

Then the only possible null entity would be imageReturnedIntent - and you either need to handle the case where it's null differently, or figure out why it's null and prevent it from being null. In either case it would be good to understand why it's null.

But the thing to remember is: use the stack trace; it's telling you exactly which line is encountering the null. Knowing that line usually points directly to the null entity; then it's your responsibility to figure out what to do about the null.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
  • correct, but i am setting the intent already as mentioned in this post: http://stackoverflow.com/questions/10473823/android-get-image-from-gallery-into-imageview following the approach in this post: http://paragchauhan2010.blogspot.ca/2012/05/choose-image-from-gallary-and-display.html ... my question is more what am i doing wrong. it should be set - as i mentioned i can set the background image of the entire application, just not the background image of a custom dialog... i'm not sure where the difference comes in, or why intent would be empty when making this change? – BigBug Nov 04 '13 at 00:12