如何在ImageButton上更改我的图像?
我有一个带有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;
}
}
现在,在其他类(公共类Galleria扩展AppCompatActivity)中,我需要使用以下命令更改imagebutton上的图像:
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();
}
但是他们给了我这个错误:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.View android.view.View.findViewById(int)'
on“bottone =(ImageButton)fragment.getView().........”
我该怎么办?