前提・実現したいこと
Android StudioでJAVAを使ったアプリ開発をしています。
ユーザー登録の際に
グーグル認証を使った新規ユーザのアカウント登録とログインをしたいのですが
どうしてもエラーが解決できません。
ユーザーがログインするLOGINとログイン後にユーザの情報が表示されるMYPAGE二つのページで構成されています。
発生している問題・エラーメッセージ
@Override
protected void onStart() {
super.onStart();
FirebaseUser user = mAuth.**getCurrentUser();** if(user!=null){ Intent intent = new Intent(getApplicationContext(), ContactsContract.Profile.class); startActivity(intent); }
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); mAuth.**signInWithCredential**(credential) .addOnCompleteListener(this, new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { if (task.isSuccessful()) { // Sign in success, update UI with the signed-in user's information FirebaseUser user = mAuth.**getCurrentUser();** Intent intent = new Intent(getApplicationContext(), ContactsContract.Profile.class); startActivity(intent); } else { Toast.makeText(LoginActivity.this, "Sorry auth failed.", Toast.LENGTH_SHORT).show(); }
上記太字の部分が解決できないメソッドとしてエラーになります。
該当のソースコード
Java
1package com.example.alertappfyp2.ui.login; 2 3 4public class LoginActivity extends AppCompatActivity { 5 6 7 private GoogleSignInClient mGoogleSignInClient; 8 private final static int RC_SIGN_IN = 123; 9 private FirebaseAuth mAuth; 10 11 12 @Override 13 protected void onStart() { 14 super.onStart(); 15 16 17 FirebaseUser user = mAuth.**getCurrentUser();** 18 if(user!=null){ 19 Intent intent = new Intent(getApplicationContext(), ContactsContract.Profile.class); 20 startActivity(intent); 21 } 22 23 24 } 25 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 31 setContentView(R.layout.activity_main); 32 33 34 mAuth = FirebaseAuth.getInstance(); 35 36 37 createRequest(); 38 39 40 findViewById(R.id.login).setOnClickListener(new View.OnClickListener() { 41 @Override 42 public void onClick(View view) { 43 signIn(); 44 } 45 }); 46 47 48 } 49 50 51 private void createRequest() { 52 53 54 // Configure Google Sign In 55 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 56 .requestIdToken(getString(R.string.default_web_client_id)) 57 .requestEmail() 58 .build(); 59 60 61 // Build a GoogleSignInClient with the options specified by gso. 62 mGoogleSignInClient = GoogleSignIn.getClient(this, gso); 63 64 65 } 66 67 68 private void signIn() { 69 Intent signInIntent = mGoogleSignInClient.getSignInIntent(); 70 startActivityForResult(signInIntent, RC_SIGN_IN); 71 } 72 73 74 @Override 75 public void onActivityResult(int requestCode, int resultCode, Intent data) { 76 super.onActivityResult(requestCode, resultCode, data); 77 78 79 // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); 80 if (requestCode == RC_SIGN_IN) { 81 Task task = GoogleSignIn.getSignedInAccountFromIntent(data); 82 try { 83 // Google Sign In was successful, authenticate with Firebase 84 GoogleSignInAccount account = (GoogleSignInAccount) task.getResult(ApiException.class); 85 firebaseAuthWithGoogle(account); 86 } catch (Throwable e) { 87 // Google Sign In failed, update UI appropriately 88 // ... 89 Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); 90 } 91 } 92 } 93 94 95 private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { 96 97 98 AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); 99 mAuth.**signInWithCredential**(credential) 100 .addOnCompleteListener(this, new OnCompleteListener() { 101 @Override 102 public void onComplete(@NonNull Task task) { 103 if (task.isSuccessful()) { 104 // Sign in success, update UI with the signed-in user's information 105 FirebaseUser user = mAuth.getCurrentUser(); 106 Intent intent = new Intent(getApplicationContext(), ContactsContract.Profile.class); 107 startActivity(intent); 108 109 110 } else { 111 Toast.makeText(LoginActivity.this, "Sorry auth failed.", Toast.LENGTH_SHORT).show(); 112 113 114 } 115 116 117 // ... 118 } 119 }); 120 } 121 122 123}
Java
1 2public class MyPage extends AppCompatActivity { 3 4 ImageView img; 5 TextView name,Age,pol; 6 Button LB; 7 8 FirebaseAuth mAuth; 9 GoogleSignInClient mGoogleSignInClient; 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_my_page); 15 16 img=findViewById(R.id.img); 17 name=findViewById(R.id.name); 18 Age=findViewById(R.id.Age); 19 pol=findViewById(R.id.pol); 20 LB=findViewById(R.id.lb); 21 22 GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this); 23 if(signInAccount != null){ 24 name.setText(signInAccount.getDisplayName()); 25 Age.setText(signInAccount.getEmail()); 26 } 27 28 29 LB.setOnClickListener(new View.OnClickListener() { 30 @Override 31 public void onClick(View view) { 32 FirebaseAuth.getInstance().signOut(); 33 Intent intent = new Intent(getApplicationContext(),MainActivity.class); 34 startActivity(intent); 35 } 36 }); 37 } 38 } 39 40 41
grade
1// Top-level build file where you can add configuration options common to all sub-projects/modules. 2 3buildscript { 4 repositories { 5 google() 6 jcenter() 7 8 } 9 dependencies { 10 classpath 'com.android.tools.build:gradle:4.1.2' 11 classpath 'com.google.gms:google-services:4.3.5' 12 13 // NOTE: Do not place your application dependencies here; they belong 14 // in the individual module build.gradle files 15 } 16} 17 18allprojects { 19 repositories { 20 google() 21 jcenter() 22 23 } 24} 25
grade
1apply plugin: 'com.android.application' 2 3android { 4 compileSdkVersion 29 5 buildToolsVersion "29.0.2" 6 defaultConfig { 7 applicationId "com.example.alertappfyp2" 8 minSdkVersion 29 9 targetSdkVersion 29 10 versionCode 1 11 versionName "1.0" 12 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 13 vectorDrawables.useSupportLibrary = true 14 } 15 buildTypes { 16 release { 17 minifyEnabled false 18 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 19 } 20 } 21} 22 23dependencies { 24 implementation fileTree(dir: 'libs', include: ['*.jar']) 25 implementation 'androidx.appcompat:appcompat:1.0.2' 26 implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 27 implementation 'com.google.android.material:material:1.0.0' 28 implementation 'androidx.annotation:annotation:1.0.2' 29 implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' 30 implementation 'androidx.preference:preference:1.1.0-alpha05' 31 implementation 'androidx.vectordrawable:vectordrawable:1.0.1' 32 implementation 'androidx.navigation:navigation-fragment:2.0.0' 33 implementation 'androidx.navigation:navigation-ui:2.0.0' 34 implementation platform('com.google.firebase:firebase-bom:26.4.0') 35 implementation 'com.google.firebase:firebase-messaging:' 36 implementation 'com.google.firebase:firebase-analytics' 37 implementation 'com.google.firebase:firebase-core' 38 implementation 'com.google.firebase:firebase-admin:7.1.0' 39 implementation 'com.google.firebase:firebase-auth:19.2.0' 40 implementation "com.firebaseui:firebase-ui-auth:7.1.1" 41 implementation 'com.google.android.gms:play-services-auth:19.0.0' 42 implementation 'com.github.bumptech.glide:glide:4.4.0' 43 testImplementation 'junit:junit:4.12' 44 androidTestImplementation 'androidx.test:runner:1.1.1' 45 androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 46} 47 48apply plugin: 'com.google.gms.google-services' 49 50
試したこと
多数の方法で試してみましたがどうしても解決できません。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー