signInWithEmailAndPassword from Activity to Fragment

时间:2018-02-12 15:40:25

标签: android android-fragments android-activity

我是Android开发的新手,我有一个问题,就是将firebase auth从SignInActivity传递给HomeFragment。这是代码

公共类SignInActivity扩展了AppCompatActivity {

private EditText inputEmail, inputPassword;
private FirebaseAuth auth;
private ProgressBar progressBar;
private Button btnSignIn, btnResetPassword;

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

    //Get Firebase Auth Instance
    auth = FirebaseAuth.getInstance();

    setContentView(R.layout.activity_sign_in);

    inputEmail = (EditText) findViewById(R.id.email);
    inputPassword = (EditText) findViewById(R.id.password);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    btnSignIn = (Button) findViewById(R.id.action_sign_in_button);
    btnResetPassword = (Button) findViewById(R.id.intent_reset_password_button);

    btnSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String email = inputEmail.getText().toString();
            final String password = inputPassword.getText().toString();

            if (TextUtils.isEmpty(email)) {
                Toast.makeText(getApplicationContext(), "Enter Email Address", Toast.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(password)) {
                Toast.makeText(getApplicationContext(), "Enter Password", Toast.LENGTH_SHORT).show();
                return;
            }

            progressBar.setVisibility(View.VISIBLE);

            //Authenticate User
            auth.signInWithEmailAndPassword(email, password)
                    .addOnCompleteListener(SignInActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            // If sign in fails, display a message to the user. If sign in succeeds
                            // the auth state listener will be notified and logic to handle the
                            // signed in user can be handled in the listener.
                            progressBar.setVisibility(View.GONE);
                            if (!task.isSuccessful()) {
                                //There was an Error
                                if (password.length() < 6) {
                                    inputPassword.setError(getString(R.string.minimum_password));
                                } else {
                                    Toast.makeText(SignInActivity.this, getString(R.string.auth_failed), Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                HomeFragment fragment = new HomeFragment();
                                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                                transaction.replace(R.id.home_fragment, fragment);
                                transaction.commit();
                            }
                        }
                    });
        }
    });
}

}

这里是HomeFragment片段

public class HomeFragment扩展了Fragment {

public HomeFragment() {
    // Required empty public constructor
}

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

}

这里是logcat

致命的例外:主要                                                                       处理:clouwiko.dev.prasiku,PID:30462                                                                       java.lang.RuntimeException:clouwiko.dev.prasiku.activity.SignInActivity@3039242e必须实现OnFragmentInteractionListener                                                                           在clouwiko.dev.prasiku.fragment.HomeFragment.onAttach(HomeFragment.java:84)                                                                           在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1363)                                                                           在android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1187)                                                                           在android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1070)                                                                           在android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:115)                                                                           在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2374)                                                                           在android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)                                                                           在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239)                                                                           在android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:700)                                                                           在android.os.Handler.handleCallback(Handler.java:739)                                                                           在android.os.Handler.dispatchMessage(Handler.java:95)                                                                           在android.os.Looper.loop(Looper.java:145)                                                                           在android.app.ActivityThread.main(ActivityThread.java:5942)                                                                           at java.lang.reflect.Method.invoke(Native Method)                                                                           在java.lang.reflect.Method.invoke(Method.java:372)                                                                           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1400)                                                                           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

点击登录后,应用程序总是崩溃,你能不能帮助我。谢谢

1 个答案:

答案 0 :(得分:0)

你的问题很清楚:

UILabel

您需要实现片段和活动的通信方式。 docs解释了你应该怎么做。此外,此guy详细说明了如何执行此操作。