4

Iv been trying to implement this shared element transition and keep getting this "java.lang.IllegalArgumentException: Shared element must not be null" error when I click to transition. Please help.

This is the MainActivity onCreate method is given below. Please check my code.

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    imageView = findViewById(R.id.image);

    recList = (RecyclerView)findViewById(R.id.RecView);
    recList.setHasFixedSize(true);

    recList.addOnItemTouchListener(
            new MyOnClickListener(this, new MyOnClickListener.OnItemClickListener() {
                @Override public void onItemClick(View view, int position) {
                    Intent intent  = new Intent(MainActivity.this, DetailsActivity.class);

                    String transitionName = getString(R.string.transitionName);



                    ActivityOptionsCompat options = ActivityOptionsCompat.
                            makeSceneTransitionAnimation(MainActivity.this, imageView, transitionName);

                    startActivity(intent, options.toBundle());
                }
            })
    );

This is the onClickListener for the recyclerView.

public class MyOnClickListener implements RecyclerView.OnItemTouchListener {

private OnItemClickListener mListener;

public interface OnItemClickListener {
    void onItemClick(View view, int position);
}

GestureDetector mGestureDetector;


@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}


public MyOnClickListener(Context context, OnItemClickListener listener) {
    mListener = listener;
    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
    View childView = view.findChildViewUnder(e.getX(), e.getY());
    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
        mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
}

The transition names for the images are the same. The bug fix here Hiccups in activity transitions with shared elements was not the issue.

The xml of my card...

<?xml version="1.0" encoding="utf-8"?>

>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="centerCrop"
        android:src="@drawable/littlefractal"
        android:transitionName="@string/transitionName" />

    <TextView
        android:id="@+id/txt_first_name"
        android:layout_width="match_parent"
        android:layout_height="20dp"

        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/image"
        android:text="FirstName"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="16sp"

       />

</RelativeLayout>

The xml of the destination activity.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

    <ImageView
        android:id="@+id/image"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:scaleType="centerCrop"
        android:src="@drawable/littlefractal"
        android:transitionName="@string/transitionName" />


</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginStart="40dp">

        <TextView
            android:id="@+id/txt_first_name_details"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:gravity="center_vertical"
            android:text="FirstName"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="16sp"
            />


        <TextView
            android:id="@+id/txt_last_name"
            android:layout_width="wrap_content"
            android:layout_height="40dp"

            android:gravity="center_vertical"
            android:text="LastName"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="16sp"
            android:layout_marginStart="10dp"/>

    </LinearLayout>




    <TextView
        android:id="@+id/txt_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:gravity="center_vertical"
        android:padding="10dp"
        android:text="Address"
        android:textColor="@color/colorPrimary"
        android:textSize="16sp"
        android:layout_marginStart="30dp"/>

    <TextView
        android:id="@+id/txt_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:gravity="center_vertical"
        android:padding="10dp"
        android:text="Phone"
        android:textColor="@color/colorPrimary"
        android:textSize="16sp"
        android:layout_marginStart="30dp"/>

</LinearLayout>

Go easy. Still learning this whole android thing. Peace and appreciation.

Community
  • 1
  • 1
tlagreca
  • 358
  • 1
  • 6
  • 20

1 Answers1

3

This exception occurs because of the fact that the imageView is null. You need getting imageView from RecyclerView item. Try this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

recList = (RecyclerView)findViewById(R.id.RecView);
recList.setHasFixedSize(true);

recList.addOnItemTouchListener(
        new MyOnClickListener(this, new MyOnClickListener.OnItemClickListener() {
            @Override public void onItemClick(View view, int position) {
                Intent intent  = new Intent(MainActivity.this, DetailsActivity.class);
                String transitionName = getString(R.string.transitionName);
                View transitionView = view.findViewById(R.id.image);
                ViewCompat.setTransitionName(transitionView, transitionName);

                ActivityOptionsCompat options = ActivityOptionsCompat.
                        makeSceneTransitionAnimation(MainActivity.this, transitionView, transitionName);

                startActivity(intent, options.toBundle());
            }
        })
);
princeparadoxes
  • 498
  • 3
  • 10