我一直试图实现这个共享元素转换并继续得到这个" java.lang.IllegalArgumentException:共享元素不能为空"单击转换时出错。请帮忙。
这是MainActivity onCreate方法,如下所示。请检查我的代码。
@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());
}
})
);
这是recyclerView的onClickListener。
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) {
}
}
图像的转换名称相同。这里的错误修复Hiccups in activity transitions with shared elements不是问题。
我卡上的xml ......
<?xml version="1.0" encoding="utf-8"?>
&GT;
<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>
目标活动的xml。
<?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>
轻松一点。还在学习这整个Android的东西。和平与欣赏。
答案 0 :(得分:2)
由于imageView为null,因此发生此异常。您需要从RecyclerView项目获取imageView。试试这个:
@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());
}
})
);