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?