I have a fragment container in main activity. Inside this fragment container, three fragments are changing and are all connected to the navigation bar, like this:
I want to open another activity when pressing button in Fragment 3 (ProfileFragment). I put the button and wrote this code in ProfileFragment.java:
newButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SettingsFragment nextFrag= new SettingsFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_profile, nextFrag)
.addToBackStack(null)
.commit();
}
});
I don't get an error while opening the application, but after clicking the button, the application closesa dn I get this error:
ScrollView can host only one direct child.
But in the XML file, ScrollView already has one child.
The XML file of the Fragment I want to open:
<ScrollView 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:background="@color/whiteColor"
tools:context=".FragmentClasses.SettingsFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="@drawable/bottomsheet_settings"
android:fontFamily="@font/inter_bold"
android:gravity="center"
android:letterSpacing="-0.03"
android:text="Settings"
android:textColor="@color/whiteColor"
android:textSize="15sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="25dp"
>
<TextView
android:id="@+id/textview1_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:fontFamily="@font/inter_semibold"
android:textColor="#686868"
android:textSize="17sp"
android:letterSpacing="-0.03"
/>
<TextView
android:id="@+id/textview2_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="John"
android:fontFamily="@font/inter_semibold"
android:textColor="#454545"
android:textSize="17sp"
android:letterSpacing="-0.03"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/profileLine"/>
</LinearLayout>
</ScrollView>
ProfileActivity XML file:
<ScrollView 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"
android:background="@color/whiteColor"
android:id="@+id/fragment_profile"
tools:context=".FragmentClasses.ProfileFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@drawable/profile__image_example"
android:scaleType="centerCrop"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:paddingTop="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="John"
android:id="@+id/nameProfile"
android:fontFamily="@font/inter_bold"
android:letterSpacing="-0.03"
android:textSize="22sp"
android:textColor="#313131"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="USA"
android:layout_below="@id/nameProfile"
android:textSize="16sp"
android:textColor="#A7A7A7"
android:letterSpacing="-0.03"
android:fontFamily="@font/inter_semibold"/>
<Button
android:id="@+id/newButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/settings_icon"
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
Main Activity XML file:
<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=".D.Main_Activity">
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="55dp"
app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/navigationBar"
app:tabRippleColor="@null"
app:tabIndicator="@null"
android:background="@drawable/dialog_holo_tablayout"
android:elevation="5dp"
app:tabMinWidth="75dp"/>
<RelativeLayout
android:id="@+id/toolbarFriendpie"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
app:layout_constraintTop_toTopOf="parent"
android:background="@drawable/dialog_holo_light_frame"
android:elevation="3dp"
>
<ImageView
android:id="@+id/mainLogo_Friendpie"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:src="@drawable/friendpie_text_logo"/>
</RelativeLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/navigationBar"
app:layout_constraintTop_toBottomOf="@+id/toolbarFriendpie" />
</androidx.constraintlayout.widget.ConstraintLayout>
And lastly, this is the logcat error:
Process: com.example.test_friendpie, PID: 16929
java.lang.IllegalStateException: ScrollView can host only one direct child
at android.widget.ScrollView.addView(ScrollView.java:446)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:887)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
What is the problem? Thank you for all.
UPDATE: SOLUTION
I changed ScrollView with RelativeLayout in my ProfileActivity and wrote ScrollView under RelativeLayout. I don't know why the previous one failed but this works.
