我是编程新手,试图在我的测试应用中实现Google Signin。 Folowing Google API文档我在gradle文件中添加了所有必需的依赖项,并通过OAuth注册了我的测试应用程序... 此外,我编写了一个代码,如Google提供的示例所述,但我的应用程序在开始时崩溃,在虚拟设备中的任何用户操作之前,甚至没有通过真实设备上的APK文件进行设置....
我做错了什么?
主要(单个)活动代码:
package ru.podgorny.carcall;
import android.accounts.Account;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
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.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.Task;
public class MainActivity extends AppCompatActivity {
SignInButton signInButton;
public static final int RC_SIGN_IN = 07;
public static final String TAG = "MainActivity";
TextView tw1;
TextView tw2;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestProfile()
.build();
GoogleSignInClient mGSC = GoogleSignIn.getClient(this, gso);
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
}
private void findViews() {
signInButton = findViewById(R.id.idButtonGoogle);
tw1 = findViewById(R.id.textView1);
tw1 = findViewById(R.id.textView2);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.idButtonGoogle:
signIn();
break;
}
}
private void signIn() {
Intent signInIntent = mGSC.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
updateUI(account);
} catch (ApiException e) {
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
updateUI(null);
}
}
private void updateUI(GoogleSignInAccount account) {
tw1.setText("OK");
tw2.setText("Name: " + account.getGivenName() + ", Family name: " + account.getFamilyName() + ", Email: " + account.getEmail() + " image: " +
account.getPhotoUrl());
}
}
Activity.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ru.podgorny.carcall.MainActivity"
>
<com.google.android.gms.common.SignInButton
android:id="@+id/idButtonGoogle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foregroundGravity="top"
android:onClick="onClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/idButtonGoogle" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView1" />
</android.support.constraint.ConstraintLayout>
默认情况下为Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.podgorny.carcall">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
控制台日志:
03-23 08:35:32.774 5682-5682/? E/libprocessgroup: failed to make and chown /acct/uid_10060: Read-only file system
03-23 08:35:32.774 5682-5682/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
03-23 08:35:32.775 5682-5682/? I/art: Not late-enabling -Xcheck:jni (already on)
03-23 08:35:33.180 5682-5682/ru.podgorny.carcall I/InstantRun: starting instant run server: is main process
03-23 08:35:33.746 5682-5682/ru.podgorny.carcall D/AndroidRuntime: Shutting down VM
03-23 08:35:33.753 5682-5682/ru.podgorny.carcall E/AndroidRuntime: FATAL EXCEPTION: main
Process: ru.podgorny.carcall, PID: 5682
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ru.podgorny.carcall/ru.podgorny.carcall.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference
at android.content.ContextWrapper.getMainLooper(ContextWrapper.java:101)
at com.google.android.gms.common.api.GoogleApi.<init>(Unknown Source)
at com.google.android.gms.auth.api.signin.GoogleSignInClient.<init>(Unknown Source)
at com.google.android.gms.auth.api.signin.GoogleSignIn.getClient(Unknown Source)
at ru.podgorny.carcall.MainActivity.<init>(MainActivity.java:35)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
03-23 08:35:33.827 5682-5695/ru.podgorny.carcall I/art: Background sticky concurrent mark sweep GC freed 2567(255KB) AllocSpace objects, 0(0B) LOS objects, 30% free, 773KB/1117KB, paused 18.376ms total 73.744ms
答案 0 :(得分:2)
试试这个
GoogleSignInOptions gso ;
GoogleSignInClient mGSC ;
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestProfile()
.build();
mGSC = GoogleSignIn.getClient(this, gso);
findViews();
}