A
A
AleAleA2018-06-22 01:57:22
Java
AleAleA, 2018-06-22 01:57:22

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);

Mistake:
Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference

OR (depending on option)
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)'

SecondLayout.xml:
<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" />

Main class has OneLayout.xml
ExampleBottomSheetDialog.class has SecondLayout.xml
In main class'e when you click on marker, ExampleBottomSheetDialog.class is activated:
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);
}

ExampleBottomSheetDialog.class:
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");
    }
}
}

You need to change the cafe image to bar.
What is the actual solution to this problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2018-06-22
@zagayevskiy

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.

E
Eugene, 2018-06-22
@klim76

What is the actual solution to this problem?

Get to know logcat better. He told you everything

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question