4

I have a fragment that is suppose to populate a background with an Imageview that is created with Picasso.

public class Fragment1 extends SherlockFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Context c = getActivity().getApplicationContext();
        View myFragmentView = inflater.inflate(R.layout.home, container, false);
        TextView text = (TextView) myFragmentView.findViewById(R.id.text);

        ImageView imageView = (ImageView) myFragmentView.findViewById(R.id.image);
        String url = ModalScreens.byId(1).getBackgroundImagePath();
        Picasso.with(c).load(url).fit().into(imageView);
        return myFragmentView;
    }
}

I can't figure out what I am doing wrong but the image wont load.

Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
user3529614
  • 41
  • 1
  • 5
  • 1
    What is being returned in the "url" String? Is it a valid path? – brwngrldev Sep 05 '14 at 00:16
  • onAttach() gives you the Activity to which the fragment is attached and you can be certain, at least reasonably so, that you have a valid activity at that point. Create a field (recommend mHost) and assign mHost = activity in onAttach() then use mHost where you need Activity context in your fragment. That will help avoid some NPE issues though that is not the answer to your question above. @adavis has the most likely cause identified. – Bill Mote Sep 05 '14 at 03:41

1 Answers1

0

From your code it seems possible that you're loading a local resource and not from the web. If the former is the case, then you could just load it like

StackOverflow showing image View from local disk

If not and you're loading a web image, then this could be a variety of problems, here's some hopefully helpful troubleshooting

  1. Have you added?

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

  1. Is the link a valid (as adavis as suggested)? Free of spaces from the beginning and end what not. I would do a

Log.wtf("Testing valid URL", "|"+url+"|")

  1. Is the image too large? In that case it might return a null Image too large Stackoverflow answer

  2. Is the Imageview properly set to display? I would confirm by initially setting it to some image like this android:src="@mipmap/large in your layout xml for your ImageView

Community
  • 1
  • 1
Ryan Zhou
  • 126
  • 1
  • 5