片段提交时出现IllegalStatementException

时间:2017-02-23 23:57:10

标签: java android android-fragments

我有以下用于测试片段事务的简单代码,因为我对Android比较陌生:首先,我只显示第一个片段,一切顺利,但是当我希望在点击按钮后更改它时,我收到以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.user.onceuponatime, PID: 3161
                  java.lang.IllegalStateException: Activity has been destroyed
                      at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1864)
                      at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:649)
                      at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:609)
                      at com.example.user.onceuponatime.activity.AuthentificationActivity.onFragmentSwapRequested(AuthentificationActivity.java:63)
                      at com.example.user.onceuponatime.fragment.SignInFragment$2.onClick(SignInFragment.java:95)
                      at android.view.View.performClick(View.java:5610)
                      at android.view.View$PerformClick.run(View.java:22265)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

活动:

public class AuthentificationActivity extends AppCompatActivity implements   SignInFragment.SignInFragmentCallBack
    ,SignUpFragment.SignUpFragmentCallBack{

public static final String SIGNINFRAGMENT_TAG = "signinfragment";
public static final String SIGNUPFRAGMENT_TAG = "signupfragment";

public static final int SIGNIN_FRAGMENT_ID = 846464;
public static final int SIGNUP_FRAGMENT_ID = 125478;
public static final int LOST_PWD_FRAGMENT_ID = 85546;

private FragmentManager mFragmentManager;
private SignInFragment mSignInFragment;
private SignUpFragment mSignUpFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_authentification);


    mSignInFragment = SignInFragment.getInstance();

    mFragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();


    fragmentTransaction.add(mSignInFragment,SIGNINFRAGMENT_TAG);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();


}

@Override
public void onFragmentSwapRequested(int FragmentId) {

    mFragmentManager = this.getSupportFragmentManager();
    FragmentTransaction fgTransaction = mFragmentManager.beginTransaction();

    switch(FragmentId) {
        case SIGNUP_FRAGMENT_ID:
            if(!isFinishing()) {
                mSignUpFragment = (SignUpFragment) mFragmentManager.findFragmentByTag(SIGNUPFRAGMENT_TAG);
                if (mSignUpFragment == null) {
                    mSignUpFragment = SignUpFragment.getInstance();
                    fgTransaction.replace(R.id.fragment_container, mSignUpFragment, SIGNUPFRAGMENT_TAG);
                } else {
                    fgTransaction.show(mSignUpFragment);
                }
                fgTransaction.addToBackStack(null);
                fgTransaction.commit();
            }
            break;
        case LOST_PWD_FRAGMENT_ID:
            break;


    }



}
}

SignUpFragment:

public class SignUpFragment extends Fragment {

FragmentSignUpBinding signUpBinding;

private EditText mEmailEdit,mPasswordEdit;
private Button btnRegister,btnLogin;
private ProgressBar mProgressBar;
private CoordinatorLayout mCoordinatorLayout;

private FirebaseAuth mAuth;

private SignUpFragmentCallBack mCallBack;

public interface SignUpFragmentCallBack {
    void onFragmentSwapRequested(int fragmentId);
}

public SignUpFragment() {
}



public static SignUpFragment getInstance() {
    SignUpFragment fragment = new SignUpFragment();

    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    signUpBinding = DataBindingUtil.setContentView(getActivity(),R.layout.fragment_sign_up);

    mEmailEdit = signUpBinding.emailSignup;
    mPasswordEdit = signUpBinding.passwordSignup;
    btnLogin = signUpBinding.loginRedirectButton;
    btnRegister = signUpBinding.registerButton;
    mProgressBar = signUpBinding.progressBarSignup;
    mCoordinatorLayout = signUpBinding.coordinatorSignup;

    mCallBack = new AuthentificationActivity();
    mAuth = FirebaseAuth.getInstance();

    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mCallBack instanceof SignUpFragmentCallBack)
                mCallBack.onFragmentSwapRequested(AuthentificationActivity.SIGNIN_FRAGMENT_ID);
        }
    });

    btnRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onRegisterClicked();
        }
    });



}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_sign_up, container, false);
}



@Override
public void onAttach(Context context) {
    super.onAttach(context);

}

private void onRegisterClicked() {
    String email = mEmailEdit.getText().toString().trim();
    final String pwd = mPasswordEdit.getText().toString().trim();

    if(TextUtils.isEmpty(email)) {
        Snackbar.make(mCoordinatorLayout,getString(R.string.no_email_entered),Snackbar.LENGTH_LONG).show();
        return;
    }

    if(TextUtils.isEmpty(pwd)) {
        Snackbar.make(mCoordinatorLayout,getString(R.string.no_password_entered),Snackbar.LENGTH_LONG).show();
        return;
    }
    mProgressBar.setVisibility(View.VISIBLE);
    mAuth.createUserWithEmailAndPassword(email,pwd).addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if(task.isSuccessful()) {
                startActivity(new Intent(getActivity(), MainActivity.class));
                getActivity().finish();
            }
            else {
                mProgressBar.setVisibility(View.GONE);
                Snackbar.make(mCoordinatorLayout,task.getException().toString(),Snackbar.LENGTH_LONG).show();
            }
        }
    });

}

@Override
public void onDetach() {
    super.onDetach();

}



}

我已经阅读了很多关于类似问题的帖子,有些人说用if(!isFinished())包装事务,其他人说要覆盖onDestroy,没有一个工作,我相信解决方案更简单。

编辑: 这是我的SignInFragment和Authentification活动xml:

SignInFragment:

public class SignInFragment extends Fragment {
FragmentSignInBinding signInBinding;

private CoordinatorLayout mCoordinatorLayout;
private EditText mEmailEdit,mPasswordEdit;
private Button btnSignIn,btnSignUp,btnLostPwd;
private ProgressBar mProgressBar;

private FirebaseAuth mAuth;

private SignInFragmentCallBack mCallBack;



public interface SignInFragmentCallBack {
    void onFragmentSwapRequested(int FragmentId);
}

public SignInFragment() {}


public static SignInFragment getInstance() {
    return new SignInFragment();
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        mCallBack = (SignInFragmentCallBack) context;
    } catch(ClassCastException e) {
        throw new ClassCastException(context.toString() + " must implements SignInCallaback");
    }
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    signInBinding = DataBindingUtil.setContentView(getActivity(),R.layout.fragment_sign_in);
    mEmailEdit = signInBinding.emailSignin;
    mPasswordEdit = signInBinding.passwordSignin;
    mProgressBar = signInBinding.progressBarSignIn;
    btnSignIn = signInBinding.signInButton;
    btnSignUp = signInBinding.signUpButton;
    btnLostPwd = signInBinding.passwordReset;
    mCoordinatorLayout = signInBinding.coordinatorSignin;



    mAuth = FirebaseAuth.getInstance();



    btnSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            signInAction();
        }
    });

    btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mCallBack instanceof SignInFragmentCallBack)
            mCallBack.onFragmentSwapRequested(AuthentificationActivity.SIGNUP_FRAGMENT_ID);
        }
    });

    btnLostPwd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mCallBack instanceof SignInFragmentCallBack)
            mCallBack.onFragmentSwapRequested(AuthentificationActivity.LOST_PWD_FRAGMENT_ID);
        }
    });


}

private void signInAction() {
    String email = mEmailEdit.getText().toString().trim();
    final String password = mPasswordEdit.getText().toString().trim();

    if(TextUtils.isEmpty(email)) {
        Snackbar.make(mCoordinatorLayout,getString(R.string.no_email_entered),Snackbar.LENGTH_LONG).show();
        return;
    }

    if(TextUtils.isEmpty(password)) {
        Snackbar.make(mCoordinatorLayout,getString(R.string.no_password_entered),Snackbar.LENGTH_LONG).show();
        return;
    }

    mProgressBar.setVisibility(View.VISIBLE);
    mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            mProgressBar.setVisibility(View.GONE);
            if(task.isSuccessful()) {
                Snackbar.make(mCoordinatorLayout,getString(R.string.login_success),Snackbar.LENGTH_LONG).show();
                startActivity(new Intent(getActivity(), MainActivity.class));
                getActivity().finish();
            }
            else
            {
                Snackbar.make(mCoordinatorLayout,getString(R.string.login_failed),Snackbar.LENGTH_LONG).show();

            }
        }
    });
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_sign_in,container,false);
}

@Override
public void onResume() {
    super.onResume();
    mProgressBar.setVisibility(View.GONE);
}
}

Authentification 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:id="@+id/activity_authentification"
    android:layout_width="match_parent"
    android:layout_height="match_parent"     tools:context="com.example.user.onceuponatime.activity.AuthentificationActivity">

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/fragment_container">


    </FrameLayout>



    </LinearLayout>

EDIT2:新的堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.user.onceuponatime, PID: 8674
                  java.lang.IllegalArgumentException: No view found for id 0x7f0d007f (com.example.user.onceuponatime:id/fragment_container) for fragment SignUpFragment{2b0b994 #1 id=0x7f0d007f signupfragment}
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1293)
                      at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                      at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
                      at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
                      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
                      at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
                      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

SignInFragment xml:

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" >
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator_signin"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.example.user.onceuponatime.fragment.SignInFragment">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="vertical"
            android:background="@color/colorPrimaryDark"
            android:padding="@dimen/activity_horizontal_margin"
            >

            <ImageView
                android:layout_width="@dimen/logo_wh"
                android:layout_height="@dimen/logo_wh"
                android:src="@mipmap/ic_launcher"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="30dp"/>

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

                <EditText
                    android:id="@+id/email_signin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/email_input"
                    android:textColor="@android:color/white"
                    android:maxLines="1"
                    android:inputType="textEmailAddress"
                    />
            </android.support.design.widget.TextInputLayout>

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

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    android:id="@+id/password_signin"
                    android:inputType="textPassword"
                    android:imeActionId="@+id/login"
                    android:imeOptions="actionUnspecified"
                    android:textColor="@android:color/white"
                    android:focusableInTouchMode="true"
                    android:hint="@string/password_input"
                    />
            </android.support.design.widget.TextInputLayout>

            <Button
                android:id="@+id/sign_in_button"
                style="?android:textAppearanceSmall"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:background="@color/colorAccent"
                android:text="@string/short_signin"
                android:textColor="@android:color/black"
                android:textStyle="bold"
                android:textAlignment="center"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/sign_up_button"
                android:background="@null"
                android:layout_marginTop="20dp"
                android:textAllCaps="false"
                android:textColor="@color/colorAccent"
                android:text="@string/link_register"

                />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/password_reset"
                android:background="@null"
                android:layout_marginTop="20dp"
                android:textAllCaps="false"
                android:text="@string/lost_password"
                android:textColor="@android:color/white"
                android:textSize="15dp"
                />


        </LinearLayout>

        <ProgressBar
            android:id="@+id/progressBarSignIn"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center|bottom"
            android:layout_marginBottom="20dp"
            android:visibility="gone" />


    </android.support.design.widget.CoordinatorLayout>
</layout>

1 个答案:

答案 0 :(得分:2)

这就是问题所在:

mCallBack = new AuthentificationActivity();

您无法使用Activity实例化new并使其正常运行。此外,无论如何,新实例将不是当前的托管实例。

相反,您希望将当前的托管Activity实例强制转换为您的界面。由于您使用的是支持Fragment,因此可以使用getActivity()getContext()来检索该实例,因为两者都将返回与Activity主机相同的对象。例如:

mCallBack = (SignUpFragmentCallBack) getContext();

这应该足以至少测试您当前的设置。但是,更清晰的实现可能是在onAttach()中使用try-catchClassCastException执行此强制转换,如果失败,我们可以抛出更具信息性的异常。

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try {
        mCallBack = (SignUpFragmentCallBack) context;
    }
    catch (ClassCastException e) {
        throw new ClassCastException(context.toString() +
            " must implement the SignUpFragmentCallBack interface");
    }
}