Answer the question
In order to leave comments, you need to log in
Actual image replacement?
Task: programmatically change the image.
I tried many options with replacing the image that is in the layout with the one that is in the drawable.
I also tried to add an image to the layout, which should replace the image from the same layout. It did not bring any result.
One of the options:
ImageView img= (ImageView) findViewById(R.id.image);
img.setImageResource(R.drawable.my_image);
Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)'
<ImageView
android:id="@+id/cafe"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="right"
android:src="@drawable/cafe" />
<ImageView
android:id="@+id/baron"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="right"
android:src="@drawable/bar" />
public boolean onMarkerClick(final Marker marker) {
ExampleBottomSheetDialog bottomSheet = new ExampleBottomSheetDialog();
bottomSheet.show(getSupportFragmentManager(), "exampleBottomSheet");
// Image replacement
if (...) {
ImageView img= (ImageView) findViewById(R.id.cafe);
img.setImageResource(R.drawable.bar);
}
public class ExampleBottomSheetDialog extends BottomSheetDialogFragment {
private BottomSheetListener mListener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.bottom_sheet_layout, container, false);
Button button1 = v.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
return v;
}
public interface BottomSheetListener {
void onButtonClicked(String text);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (BottomSheetListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement BottomSheetListener");
}
}
}
Answer the question
In order to leave comments, you need to log in
The question has nothing to do with setting the image. You're trying to find a view where there isn't one. Check that there is a view with this ID in the corresponding layout file.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question