无法将片段添加到活动onClick按钮上

时间:2018-01-27 16:52:07

标签: android xml android-fragments android-activity view

当我点击按钮时,我正试图在我的活动视图旁边显示一个片段,但我一直有这个错误,我无法解决这个问题。 java.lang.IllegalArgumentException: No view found for id 0x7f090081 (nofix.oc:id/register_form_container) for fragment FragmentRegisterClient{6a297ba #0 id=0x7f090081}

我用截图来解释你想要发生的事情:http://prntscr.com/i6dx9f

以下是我正在调用我的片段的活动的代码:

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();
        } 

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

这是我的片段的代码和XML:

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

    }

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

修改: 我不得不删除第二个setContentView()以使其工作。

0 个答案:

没有答案