-3

Errors:

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

or

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" />

The first class has OneLayout.xml

ExampleBottomSheetDialog.class has SecondLayout.xml

In the first class, when you click on the 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);
    }

How to resolve issue with null object?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

2

You try to access the ImageView of the ExampleBottomSheetDialog whereas the view is not created (so the ImageView is null). You must give arguments to your ExampleBottomSheetDialog and manage the ImageView resource drawable in the ExampleBottomSheetDialog class (in the onCreateDialog method for example).

Bubu
  • 1,533
  • 14
  • 14