无法解析方法updateUI()

时间:2019-04-15 21:07:41

标签: android firebase

我正在按照Google登录的官方文档,将带有Firebase的google登录名添加到我的项目中。

https://developers.google.com/identity/sign-in/android/sign-in

我已经看过其他问题,但是没人能解决我的问题。我不知道是必须创建自己的updateUI()方法还是必须添加一些库,并且如果必须编写它,该怎么办。代码简短,简单

package com.database.gabriele.databasefb;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.SignInButton;
import com.google.firebase.auth.FirebaseAuth;


public class MainActivity extends AppCompatActivity {

    SignInButton button;
    FirebaseAuth mAuth;
    private final static int RC_SIGN_IN = 2;
    GoogleSignInClient mGoogleSignInClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

        button = (SignInButton) findViewById(R.id.sign_in_button);
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
        updateUI(account);
    }
}

当我尝试运行代码时,它返回错误“无法解析方法updateUI(...)”

3 个答案:

答案 0 :(得分:3)

那是因为您的班级中没有此方法,因此需要根据需要创建它。该文档仅提到在应用程序启动时需要进行一些UI更新。

例如-检查用户是否已经登录并做相应的事情:

  //Change UI according to user data.
public void  updateUI(FirebaseUser account){
    if(account != null){
        Toast.makeText(this,"U Signed In successfully",Toast.LENGTH_LONG).show();
        startActivity(new Intent(this,AnotherActivity.class));
    }else {
        Toast.makeText(this,"U Didnt signed in",Toast.LENGTH_LONG).show();
    }
}

答案 1 :(得分:1)

这是一个很有趣的问题,如果您阅读文档,就会使您感到困惑。 假设您想使用您的应用程序匿名注册用户。您可以使用此代码

   final  FirebaseAuth mAuth;
        mAuth = FirebaseAuth.getInstance();
        mAuth.signInAnonymously()
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInAnonymously:success");
                            String user = mAuth.getCurrentUser().toString();
                            updateUI(user);
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInAnonymously:failure", task.getException());
                            Toast.makeText(getApplicationContext(), "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                            updateUI(null);
                        }

                        // ...
                    }
                });

所以这行

String user = mAuth.getCurrentUser().toString();

将为您提供当前用户返回的字符串。

您现在可以将其传递给方法updateUI(user)并使用它来执行所需的操作。 1.看看是否有价值 2.记录值 3.等 为此,您需要编写方法

    updateUI(String user)
{
Toast.makeText(MainActivity.this, user+" <- this is the user", Toast.LENGTH_LONG).show();
}

我知道这是一个简单的例子。我总是在寻找简单的例子,而不是复杂的解决方案。

答案 2 :(得分:0)

UpdaeUI()方法仅用于更新具有用户当前身份验证状态的用户界面,例如,如果用户已经登录,则是否已经登录,然后将用户重定向到仪表板活动屏幕,否则将其重定向到登录屏幕。

{{1}} >

在执行此操作时,我有一个相同的问题,为了简化操作,我创建了一个仅用于使用Firebase进行用户身份验证的应用程序。您可以使用以下链接在github上查看我的代码。我希望这可以帮助您理解。

https://github.com/Vijay-Tahelramani/Android_Firebase_Authentication

它将允许您创建登录,注册,忘记和更改密码以及自动注销功能。它还允许您发送用户名和个人资料图片。