Java 膨胀DialogFragment时出错:指定的子级已具有父级。必须对子对象&x27;调用removeView();她先是父母
在尝试膨胀包含RecyclerView的FragmentDialog时,我遇到了这样一个错误:“指定的子项已具有父项。您必须首先对子项的父项调用removeView()。” 以下是我的DialogFragment中的代码:Java 膨胀DialogFragment时出错:指定的子级已具有父级。必须对子对象&x27;调用removeView();她先是父母,java,android,Java,Android,在尝试膨胀包含RecyclerView的FragmentDialog时,我遇到了这样一个错误:“指定的子项已具有父项。您必须首先对子项的父项调用removeView()。” 以下是我的DialogFragment中的代码: public class MyDialogFragment extends AppCompatDialogFragment { private static final String DATA = "data"; private MyD
public class MyDialogFragment extends AppCompatDialogFragment {
private static final String DATA = "data";
private MyDialogFragment.MyDialogFragmentListener listener;
private ArrayList<MyArrayList> mArrayListData;
public static MyDialogFragment newInstance(ArrayList<MyArrayList> ArrayListData) {
Bundle args = new Bundle();
MyDialogFragment fragment = new MyDialogFragment();
args.putParcelableArrayList(DATA, ArrayListData);
fragment.setArguments(args);
return fragment;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
if (getArguments() != null) {
mArrayListData = getArguments().getParcelableArrayList(DATA);
}
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setView(createRecyclerView(mArrayListData))
.setTitle(requireContext().getString(R.string.title))
.setPositiveButton(requireContext().getString(R.string.ok_button), null);
Dialog dialog = builder.create();
/* Option set cause key board doesn't appear */
dialog.show();
Objects.requireNonNull(dialog.getWindow()).clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
@Override
public void onResume() {
super.onResume();
final AlertDialog MyAlertDialog = (AlertDialog) getDialog();
if (MyAlertDialog != null) {
Button positiveButton = MyAlertDialog.getButton(Dialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...
dismiss();
}
});
}
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
listener = (MyDialogFragment.MyDialogFragmentListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() +
"must implement ExampleDialogListener");
}
}
public interface MyDialogFragmentListener {
void MyDialogFragmentListenerReturn(ArrayList<MyArrayList> ArrayListData);
}
private RecyclerView createRecyclerView(final ArrayList<MyArrayList> ArrayListData){
LayoutInflater inflater = requireActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.fragmentLayout, null, false);
RecyclerView MyRecyclerView = view.findViewById(R.id.recyclerViewId);
MyRecyclerAdapter myAdapter = new MyRecyclerAdapter (getActivity(), ArrayListData);
MyRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
MyRecyclerView.setAdapter(myAdapter);
myAdapter.setOnItemClickListener(new MyRecyclerAdapter.OnItemClickListener() {
@Override
public void onReturnValue(int position, int value) {
mArrayListData.get(position).setValue(value);
}
});
return MyRecyclerView;
}
}
它将问题指向我的DialogFragment的“dialog.show();”。我得承认,我完全迷路了。我有另一个DialogFragment,它以同样的方式工作,没有问题。我解决了我的问题,我最初认为它来自我的java代码,但它是一个xml布局问题 我的DialogFrament布局如下:
<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="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FF8200"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MyDialogFragment ">
<TextView
android:id="@+id/textViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewId"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewId" />
</androidx.constraintlayout.widget.ConstraintLayout>
我只需要使用RecyclerView作为布局的唯一元素:
<androidx.recyclerview.widget.RecyclerView 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:id="@+id/recyclerViewId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="#FF8200"
android:nestedScrollingEnabled="false"
android:padding="16dp"
tools:context=".MyDialogFragment "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
我解决了我的问题,我最初认为它来自我的java代码,但这是一个xml布局问题 我的DialogFrament布局如下:
<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="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FF8200"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MyDialogFragment ">
<TextView
android:id="@+id/textViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewId"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewId" />
</androidx.constraintlayout.widget.ConstraintLayout>
我只需要使用RecyclerView作为布局的唯一元素:
<androidx.recyclerview.widget.RecyclerView 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:id="@+id/recyclerViewId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="#FF8200"
android:nestedScrollingEnabled="false"
android:padding="16dp"
tools:context=".MyDialogFragment "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
我在对话框fragment的createRecyclerView函数中尝试了这一点:查看视图=充气机.inflate(R.layout.fragmentLayout,null,false);if(view!=null){ViewGroup parent=(ViewGroup)view.getParent();if(parent!=null){parent.removeView(view);}}
不起作用我在对话框fragment的createRecyclerView函数中尝试过:view view=inflater.inflate(R.layout.fragmentLayout,null,false);if(view!=null){ViewGroup parent=(ViewGroup)view.getParent();if(parent!=null){parent.removeView(view);}}
它不工作