0

I'm trying to make a fragment appear next to my activity view when I click on a button, but I keep having this error and I can't solve this. java.lang.IllegalArgumentException: No view found for id 0x7f090081 (nofix.oc:id/register_form_container) for fragment FragmentRegisterClient{6a297ba #0 id=0x7f090081}

I took a screenshot to explain you what I want to happen : http://prntscr.com/i6dx9f

Here is the code of the Activity I'm calling my Fragment from :

public class RegisterActivity extends AppCompatActivity{
[...]
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

  tmpSavedInstanceState = savedInstanceState;


    setContentView(R.layout.activity_register);
[...]
FrameLayout frame = new FrameLayout(this);
        frame.setId(CONTENT_VIEW_ID);
        setContentView(frame, new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT));


        if (tmpSavedInstanceState == null) {
            Fragment newFragment = new FragmentRegisterClient();
            Log.e("error","eezrzezeerez");
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.register_form_container, newFragment).commit();
        } 

Here is the layout f RegisterActivity:

 <LinearLayout 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:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="nofix.Links.RegisterActivity">



    <!-- Login progress -->

    <ProgressBar
        android:id="@+id/register_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:visibility="gone" />

    <ScrollView
        android:id="@+id/register_form"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/email_register_form"
            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:weightSum="1"
                android:layout_marginBottom="15dp">

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="104dp"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="50dp"
                    android:layout_weight="0.80"
                    android:text="@string/account_type"
                    android:textSize="20sp"
                    app:layout_constraintRight_toLeftOf="@+id/dropdown_account_type"
                    tools:layout_editor_absoluteY="8dp" />

                <Spinner
                    android:id="@+id/dropdown_account_type"
                    android:layout_width="179dp"
                    android:layout_height="wrap_content"
                    tools:layout_editor_absoluteY="8dp"
                    tools:layout_editor_absoluteX="157dp"
                    android:layout_weight="0.67" />
            </LinearLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <EditText
                android:id="@+id/name_register"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/prompt_name"
                android:imeActionId="@+id/login"
                android:imeActionLabel="@string/action_register"
                android:imeOptions="actionUnspecified"
                android:maxLines="1" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/first_name_register"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/prompt_first_name"
                    android:imeActionId="@+id/login"
                    android:imeActionLabel="@string/action_register"
                    android:imeOptions="actionUnspecified"
                    android:maxLines="1" />

            </android.support.design.widget.TextInputLayout>


            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <AutoCompleteTextView
                    android:id="@+id/email_register"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/prompt_email"
                    android:inputType="textEmailAddress"
                    android:maxLines="1" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/password_register"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/prompt_password"
                    android:imeActionId="@+id/login"
                    android:imeActionLabel="@string/action_register"
                    android:imeOptions="actionUnspecified"
                    android:inputType="textPassword"
                    android:maxLines="1" />

            </android.support.design.widget.TextInputLayout>

            <Button
                android:id="@+id/email_register_form_button"
                style="?android:textAppearanceSmall"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="@string/action_register"
                android:textStyle="bold" />

        </LinearLayout>

    </ScrollView>
    <FrameLayout
        android:id="@+id/register_form_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </FrameLayout>
</LinearLayout>

And here is code and XML of my fragment :

public class FragmentRegisterClient extends Fragment {


        private View view;


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            Log.e("First test", "Please tell me it's ok");
            // Inflate the layout for this fragment
            view = inflater.inflate(R.layout.fragment_register_client, container, false);
            Log.e("Second Test", "NICE");
            return view;
        }

    }

Here is the XML

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="nofix.Links.FragmentRegisterClient">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bite"/>

</FrameLayout>

EDIT: I had to remove the second setContentView() to make it work.

Nark
  • 454
  • 1
  • 7
  • 18
  • Possible duplicate of [Android Fragment no view found for ID?](https://stackoverflow.com/questions/7508044/android-fragment-no-view-found-for-id) – ADM Jan 27 '18 at 16:53
  • I've already looked at this post, and I didn't find an appropriate answer. – Nark Jan 27 '18 at 16:59
  • 1
    You have called `setContentView()` twice why ? This is the reason. – ADM Jan 27 '18 at 17:03
  • Isn't the second one adding a custom view for the fragment ? – Nark Jan 27 '18 at 17:06
  • Just removed it, it works just fine. I've been trying to fix that for 3hours, you saved my day man, thank you ! – Nark Jan 27 '18 at 17:09

0 Answers0