Mistake in TransitionAnimation - "Shared element must not be null"
-
Mistake:
Process: pc.dd.fragment_animation, PID: 22407 java.lang.IllegalArgumentException: Shared element must not be null ...at pc.dd.fragment_animation.MainClass$1.onClick(MainClass.java:91)
Xml main activate:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="pc.dd.fragment_animation.MainClass"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="fill_parent" android:layout_height="200dp" android:transitionName="source2" android:id="@+id/source" android:layout_gravity="center_horizontal" android:src="@drawable/image1" android:scaleType="center" /> </LinearLayout>
Code 2 activates which we move after animation:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="wrap_content" android:layout_height="200dp" android:id="@+id/image_target" android:layout_gravity="center_horizontal" android:transitionName="tar" android:src="@drawable/image1" android:scaleType="center" />
Code onCreate:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_class); final View photo = findViewById (R.id.image_target); final ImageView image = (ImageView) findViewById(R.id.source);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainClass.this, Second_Activity.class);
intent.setAction(intent.ACTION_VIEW);
intent.putExtra(MainClass.this.EXTRA_PHOTO, R.drawable.image1);
//String transitionName = (String) findViewById(R.id.image_target);
startActivity(intent,
ActivityOptions.makeSceneTransitionAnimation(
MainClass.this,
photo
,
"tar"
).toBundle());
}
});
What's wrong?
-
You don't have an image-target element on the main activist, and you're trying to find it and transfer it to the transition anaemia. If the source element is to turn into a new activism in image_target, you should turn it into animation. Yeah, and I think the transition will work if it's just for the main activist to change.
android:transitionName="source2"
♪android:transitionName="tar"
and the code generating ActivityOptions.