活动无效的片段交易

时间:2016-05-28 07:50:43

标签: java android android-fragments

所以我有一个主要活动,它提交片段的交易,使用图像视图将活动膨胀,就像一个主要封面,并使用“注册”按钮将您带到signUp活动。然而,所有这些确实有效,交易起作用,而且inflater也是如此,但问题出现在:

我试图从活动(signUp)调用片段(signUpForm)。问题的根源是signUpForm的片段事务不起作用,因为它上面编写的toast没有显示,所以它甚至没有到达inflater。

signUp class:

    package com.example.joshumberto.workcon;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.Toast;

    public class signUp extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);

            getSupportFragmentManager().beginTransaction()
                    .add(R.id.sign_up_container, new signUpForm(), "signUpForm").commit();

    }
    }

activity_sign_up.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
    android:orientation="vertical"
    android:id="@+id/sign_up_container"
    tools:context=".signUp" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="look, i should have inflated companions"
        />

     </LinearLayout>

signUpForm类(仅粘贴oncreate方法,不需要其他内容):

package com.example.joshumberto.workcon;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class signUpForm extends Fragment implements View.OnClickListener {

    EditText correo;
    EditText telefono;
    Button continuar;

    public signUpForm(){
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //   setHasOptionsMenu(true);
    }

    public View OnCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        Toast.makeText(
                getActivity(),
                "i am supposued to be created!!",
                Toast.LENGTH_LONG).show();

        View v = inflater.inflate(R.layout.fragment_sign_up_form, container, false);

        correo = (EditText) v.findViewById(R.id.txt_usuario_correo);
        telefono = (EditText) v.findViewById(R.id.txt_usuario_telefono);
        continuar = (Button) v.findViewById(R.id.btn_registro_continuar);
        continuar.setOnClickListener(this);

        return v;
    }

fragment_sign_up_form.xml(只是按钮,文本视图和编辑文本以膨胀活动

<LinearLayout 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"
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"
android:background="@color/colorSignUpBackground"
tools:context=".signUp"
android:id="@+id/sign_up_form" >

<!--<include android:id="@+id/toolbar"
    layout="@layout/toolbar" />-->

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Registro"
    android:fontFamily="sans-serif-smallcaps"
    android:textSize="@dimen/titulo_txt"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="@dimen/margin_top_standar" />

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txt_usuario_correo"
        android:layout_marginTop="@dimen/margin_top_login_btn"
        android:hint="Correo"
        android:gravity="center"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txt_usuario_contrasena"
        android:layout_marginTop="@dimen/margin_top_login_btn"
        android:hint="Contrasena"
        android:gravity="center"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txt_usuario_telefono"
        android:layout_marginTop="@dimen/margin_top_login_btn"
        android:hint="Telefono"
        android:gravity="center"
        android:layout_gravity="center_horizontal" />

</LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Continuar"
    android:id="@+id/btn_registro_continuar"
    android:layout_gravity="center_horizontal" />

<!--<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/miLista"
    ></ListView>-->

想法?感谢。

1 个答案:

答案 0 :(得分:0)

您应该尝试使用replace()方法而不是add()。像这样。

getSupportFragmentManager().beginTransaction().replace(R.id.sign_up_container, new signUpForm(), "signUpForm").commit();