Good Daay,
我想在一个Fragment中获得ConstraintLayout,但是在调试它时会先后这个错误`
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int androidx.constraintlayout.widget.ConstraintLayout.getChildCount()'
在此行
constraintSet.clone(constraintLayout);
所以这是我用于该片段的活动,并且在该片段的代码下方
片段活动:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.something.fragments.Fragment"
android:id="@+id/this_fragment">
...
</androidx.constraintlayout.widget.ConstraintLayout>
这里是我的代码:
public class Fragment extends AppCompatDialogFragment {
View view;
@SuppressLint("InflateParams")
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(Objects.requireNonNull(getActivity()), R.style.Some_Theme_Name);
LayoutInflater inflater = getActivity().getLayoutInflater();
view = inflater.inflate(R.layout.fragment_activity, null);
ConstraintLayout constraintLayout = getActivity().findViewById(R.id.this_fragment);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
return builder.create();
}
}
非常感谢您的帮助
ConstraintLayout constraintLayout = view.findViewById(R.id.this_fragment);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);