gpt4 book ai didi

android - 共享元素不能为空 - 共享元素 Activity 转换

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:14:18 25 4
gpt4 key购买 nike

我一直在尝试实现此共享元素转换,并在我单击转换时不断收到此“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"?>

>

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

目标 Activity 的 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 的东西。和平与赞赏。

最佳答案

出现此异常是因为 imageView 为空。您需要从 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());
}
})
);

关于android - 共享元素不能为空 - 共享元素 Activity 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39652547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com