Crunch
Crunch

Reputation: 2823

Android AlertDialog force close

I have used the code below in a different activity in the same app with no problems, but in this activity, I get a force close when clicking the image. The event handler fires, as I see the toast, if I don't call the popupImage(). In the debugger, I get a Class cast exception, but I'm not sure how to fix it.

picture.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            // Show popup with full image of the clicked small img.
            Toast.makeText(getApplicationContext(), "debug CLICKED IMG",
                    Toast.LENGTH_LONG).show();
            popupImage(show);//fungerer stadig ikke
        }
    });
}

and

private void popupImage(Show show) {
    Context context = getApplicationContext();
    // ImgHandler imageHandler = new ImgHandler();
    // Bitmap bitmapImage = imageHandler.getImg(show.getShowId()) ;
    AlertDialog.Builder imageDialog = new AlertDialog.Builder(context);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.custom_fullimage_dialog, (ViewGroup) ((Activity) context).findViewById(R.id.layout_root));
    ImageView image = (ImageView) layout.findViewById(R.id.fullimage);
    image.setImageBitmap(pic);
    imageDialog.setView(layout);
    imageDialog.setPositiveButton(getString(R.string.Close),
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    imageDialog.create();
    imageDialog.show();
}

Upvotes: 0

Views: 774

Answers (2)

user370305
user370305

Reputation: 109257

try

   View layout = inflater.inflate(R.layout.custom_fullimage_dialog, (ViewGroup)Activityname.this .findViewById(R.id.layout_root));  

instead of

   View layout = inflater.inflate(R.layout.custom_fullimage_dialog, (ViewGroup) ((Activity) context).findViewById(R.id.layout_root)); 

Upvotes: 0

Femi
Femi

Reputation: 64700

Without the full logcat output it is difficult to tell specifically what you are doing wrong, but I expect your problem is here:

View layout = inflater.inflate(R.layout.custom_fullimage_dialog, (ViewGroup) ((Activity) context).findViewById(R.id.layout_root));

You try and cast context to an Activity, but you get it here:

Context context = getApplicationContext();

So that's probably not an Activity.

Upvotes: 1

Related Questions