在另一个ImageView的回调中配置ImageView

时间:2017-03-21 18:45:46

标签: android imageview picasso

我需要使用ImageView位图配置ImagePrincipal(imageBackground),但在将位图传递给NullPointerException

时会出现错误(imageBackground
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
    ViewPager pager = (ViewPager) container;
    View view = LayoutInflater.from(context).inflate(R.layout.slider_image_perfil_adapter, null);
    // final ProgressBar progressBar =  (ProgressBar) view.findViewById(R.id.progressBar2);
    final ImageView imagePrincipal = (ImageView) view.findViewById(R.id.imagePrincipal);
    final ImageView imageBackground = (ImageView) view.findViewById(R.id.imagebackground);

    pager.addView(view);
    if (!images.get(position).getUrl().equals("")) {
        Picasso.with(context).load(images.get(position).getUrl()).fit().centerInside().into(imagePrincipal, new Callback() {
            @Override
            public void onSuccess() {
                try {
                    if (imagePrincipal.getDrawable() != null) {
                        Bitmap bitmap = ((BitmapDrawable) imagePrincipal.getDrawable()).getBitmap();
                        imagePrincipal.setImageBitmap(bitmap);

                        if (!isExibicaoPortifolioActivity) {
                            bitmap = criarEfeitoBlur(bitmap, 25.0 f);
                            if (bitmap != null) {
                                //Drawable drawable = new BitmapDrawable(imagePrincipal.getContext().getResources(), bitmap);
                                //imagePrincipal.setBackground(drawable);

                                imageBackground.setImageBitmap(bitmap); //This erro ocurred in this line 
                            }
                        }
                    }
                } catch (Exception ex) {
                    Toast.makeText(context, ex.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError() {
                Log.e("Picasso", "onError: ");
            }
        });
    }

    return view;
}

1 个答案:

答案 0 :(得分:0)

public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } 中设置位图时,您获得了[Authorize] public class HomeController : Controller { public ActionResult Index() { return View((User as ClaimsPrincipal).Claims); } } 。根据您的代码,您检查NullPointerException是否为空。所以怀疑你的ImageView是空的。

确保Bitmap的ID正确无误且存在于ImageView布局中。