2011-08-28 117 views
0

我已经在同一个应用程序的不同活动中使用了下面的代码,没有任何问题,但是在此活动中,单击图像时会得到一个关闭的力量。事件处理程序会在我看到吐司时触发,如果我不调用popupImage()。在调试器中,我得到一个类抛出异常,但我不知道如何解决它。Android AlertDialog force关闭

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

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(); 
} 
+0

请提供确切的错误信息,并指出提高了'ClassCastException'的准确代码行。 – CommonsWare

回答

0

尝试

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

,而不是

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

谢谢大家的意见。这证明可以解决它。 – Crunch

1

没有充分logcat的输出也很难明确告诉你在做什么错的,但我希望你的问题是在这里:

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

你尝试投contextActivity,但你得到它在这里:

Context context = getApplicationContext(); 

所以这可能不是一个活动。