1

How can I change my Image on ImageButton?

I Have a fragment with imageButton:

 public class CreaStanzaFragment extends Fragment {

   public CreaStanzaFragment(){}


ImageButton icon;


public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_log_creastanza,      container, false);

    icon = (ImageButton)rootView.findViewById(R.id.newicon);
    icon.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            startActivity(new Intent(getContext(),Galleria.class));

        }
    });

    return rootView;
}

}

Now, in other class ( public class Galleria extends AppCompatActivity) i need to change image on imagebutton with:

    Fragment   fragment = new CreaStanzaFragment();


            if (fragment != null) {

                bottone = (ImageButton)    fragment.getView().findViewById(R.id.newicon);
                Image image = images.get(position);

                Glide.with(view.getContext()).load(URL)
                        .thumbnail(0.5f)
                        .crossFade()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(bottone);

                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, fragment).commit();

       }

But they give me this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

on "bottone = (ImageButton) fragment.getView() ........."

What can I do?

andreas
  • 16,357
  • 12
  • 72
  • 76
francesco bocci
  • 69
  • 2
  • 11
  • Try this code `View fragmentView = getView(); if(fragmentView != null) { // ... }` instead of `if (fragment != null) {` – pz64_ Sep 17 '16 at 18:47
  • getView() will return a view in between calls to onCreateView() and onDestroyView(). Outside of that, the Fragment can still exist in memory, but not attached to any View, thus getView() will return null – pz64_ Sep 17 '16 at 18:49
  • `View fragmentView = getView();` is incorrect becouse when i use "if" i'm not in Fragment class, i'm in Galleria (appcompatactivity).. but I do `View fragmentView = fragment.getView();` and with `if(fragmentView != null)` he don't go in "if".. why view is null? impossible.. – francesco bocci Sep 17 '16 at 19:01
  • in your code , even fragment isn't null it's view is null. Since fragment has been detached from the view. so you need to attach it if it is null. So check is the view is null if it is null then attach it . otherwise use it without attaching. – pz64_ Sep 17 '16 at 19:22
  • but how can i attach it? – francesco bocci Sep 17 '16 at 19:25
  • Can you show me an exemple with my fragment? – francesco bocci Sep 17 '16 at 19:26
  • [see this post](http://stackoverflow.com/questions/15136409/getview-returns-null) Read all the comments also – pz64_ Sep 17 '16 at 19:28
  • I create `View fragmentView = getView();` in my Fragment, but how can i call fragmentView in other class? – francesco bocci Sep 17 '16 at 19:33
  • no you need to do it like `View fragmentView = fragment.getView()` Sorry you was correct at that point – pz64_ Sep 17 '16 at 19:36
  • `View fragmentView = fragment.getView().findViewById(R.id.newicon); if(fragmentView != null) { bottone = (ImageButton) fragmentView; Image image = images.get(position); Glide.with(view.getContext()).load(URL) .thumbnail(0.5f) .crossFade() .diskCacheStrategy(DiskCacheStrategy.ALL) .into(bottone); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.frame_container, fragment).commit(); }` – pz64_ Sep 17 '16 at 19:37
  • i do it but don't work.. is null D: – francesco bocci Sep 17 '16 at 19:38
  • I'm not sure about the code above . It was just my guess according to the other post that I've commented above – pz64_ Sep 17 '16 at 19:38
  • but now the error is in ` View fragmentView = fragment.getView().findViewById(R.id.newicon);` – francesco bocci Sep 17 '16 at 19:41
  • What about this code `View fragmentView = fragment.getView(); if(fragmentView != null) { bottone = (ImageButton) fragmentView.findViewById(R.id.newicon); Image image = images.get(position); Glide.with(view.getContext()).load(URL) .thumbnail(0.5f) .crossFade() .diskCacheStrategy(DiskCacheStrategy.ALL) .into(bottone); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.frame_container, fragment).commit(); }` – pz64_ Sep 17 '16 at 19:47
  • Null point again.. i read that i can't do "getview" if im not in fragment, i need to use a method so i create method 'public View vista(){ return getView() }' in Fragment class.. but now how can i call this method in other class? – francesco bocci Sep 17 '16 at 19:55
  • create an object of CreaStanzaFragment and call the function vista() from its object. – pz64_ Sep 17 '16 at 19:58
  • Or what happens if you create `static view v` as member variable of CreaStanzaFragment and assign the rootview inside onCreateView() function as `this.v = rootview` and use that view in your Galleria class like `CreaStanzaFragment.v. findViewById()` – pz64_ Sep 17 '16 at 20:09
  • I create an object of fragment but i can't call static v or another method of fragment. idk why. – francesco bocci Sep 18 '16 at 10:03

0 Answers0