繁体   English   中英

应用程序在单击 Google 登录按钮时关闭

[英]App Closes On Clicking Google SIgn In Button

我试图将谷歌登录集成到我的应用程序中。 当我单击按钮时,应用程序关闭(而不是崩溃)。 我在这里犯了什么错误? 我尽力遵循谷歌开发者网站告诉我的内容。 我提前为长代码道歉。

我的 Java 代码:

      import static com.google.android.gms.common.SignInButton.COLOR_DARK;

    public class User_Name extends AppCompatActivity implements
            GoogleApiClient.OnConnectionFailedListener{
        private PrefManager2 prefManager;
        SignInButton signInButton;
        String email, name;
        LoginButton loginButton;
        GoogleApiClient mGoogleApiClient;

        private static final int RC_SIGN_IN = 9001;
        SQLiteDatabase db;
        private GoogleSignInOptions googleSignInOptions;


        private static final String EMAIL_PATTERN = "^[a-zA-Z0-9#_~!$&'()*+,;=:.\"(),:;<>@\\[\\]\\\\]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*$";
        private Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        private Matcher matcher; //validate email

        Button normalLogin;

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


            prefManager = new PrefManager2(this);
            if (!prefManager.isFirstTimeLaunch()) {
                launchHomeScreen(); //custom method
                finish();
            }


            setContentView(R.layout.activity_user__name);




            googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().requestId().build();
            mGoogleApiClient  = new GoogleApiClient.Builder(this).enableAutoManage(this,this).addApi(Auth.GOOGLE_SIGN_IN_API,googleSignInOptions).build();

            SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button); //google Integration
            signInButton.setColorScheme(COLOR_DARK);
            signInButton.setSize(SignInButton.SIZE_WIDE);


            signInButton.setScopes(googleSignInOptions.getScopeArray());


            signInButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
                    startActivityForResult(signInIntent, RC_SIGN_IN);
                }
            });

    //________________________________________________________________________________________________________________________


    //____________________________________________________________________________________________________







        }


        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);


            if(requestCode == RC_SIGN_IN){
                GoogleSignInResult googleSignInResult = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
                GoogleSignInAccount googleSignInAccount = googleSignInResult.getSignInAccount();

                try{
                    email =  googleSignInAccount.getEmail();
                    name = googleSignInAccount.getDisplayName();

                    //-------------
                    Beacon_Database Beacon_Database = new Beacon_Database(User_Name.this);
                    db = Beacon_Database.getReadableDatabase();

                    //----------------try-----------




                    if(email.equals("")){
                        email = "None";
                    }

                    ContentValues values1 = new ContentValues();
                    values1.put("NAME",name);
                    values1.put("EMAIL",email);


                    db.insert("LOGIN",null,values1);

                    Toast.makeText(User_Name.this,email, Toast.LENGTH_LONG).show();

                }catch(NullPointerException exc){
                    Toast.makeText(User_Name.this,"Something Seems To Have Gone Wrong",Toast.LENGTH_LONG).show();
                }

            }
        }

        private void launchHomeScreen() {
            prefManager.setFirstTimeLaunch(false);
            Intent intent = new Intent(User_Name.this,UserLocation2.class);
            startActivity(intent);
            finish();
        }


        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
            AlertDialog.Builder alertdialog = new AlertDialog.Builder(User_Name.this);
            alertdialog.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
            alertdialog.setTitle("Error");
            alertdialog.setMessage("Please Check Your Network Connection");
            alertdialog.show();
        }
}

我的布局 xml 与登录按钮:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:fb="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="56dp"
        android:background="@drawable/attempt"
        android:paddingLeft="24dp"
        android:paddingRight="24dp"
        android:weightSum="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="72dp"
           android:layout_marginBottom="16dp"
            android:layout_gravity="center_horizontal"
            android:textAppearance="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large"
            android:text="Beacon"
            android:fontFamily="casual"
            android:textColor="#e0e0e0"
            android:textStyle="normal|bold|italic"
            android:typeface="sans"
            android:textAllCaps="false"
            android:textSize="65sp"
            android:id="@+id/textView" />



        <android.support.design.widget.TextInputLayout
            android:id = "@+id/namewrapper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:animateLayoutChanges="true"
            android:textColorHint="#ffffff"
            android:layout_marginBottom="8dp">

            <EditText android:id="@+id/input_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPersonName"
                android:animateLayoutChanges="true"
                android:textColorHint="#ffffff"
                android:textColor="#ffffff"
                android:hint="Name" />
        </android.support.design.widget.TextInputLayout>




        <android.support.design.widget.TextInputLayout
            android:id = "@+id/emailwrap"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:textColorHint="#ffffff"
            android:layout_marginBottom="8dp">
            <EditText android:id="@+id/input_email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="Email" />
        </android.support.design.widget.TextInputLayout>
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone"
            android:id = "@+id/authProgress"/>





        <!-- Signup Button -->
        <Button
            android:id="@+id/btn_login"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginBottom="24dp"
            android:padding="12dp"
            android:background="#004D40"
            android:textColor="#e0e0e0"
            android:text="Create Account"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#ffffff"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OR"
            android:layout_gravity="bottom|center"
            android:paddingTop="8dp"
            android:textSize = "40sp"
            android:textColor="#ffffff"
            android:fontFamily="casual" />





<com.google.android.gms.common.SignInButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id = "@+id/sign_in_button"


    />



    </LinearLayout>


</FrameLayout>

这可能相关也可能不相关,但请检查您的 Activity 是否未设置为 SingleTask Activity。

某些设备在执行单独的任务时无法从 Google UI/库正确返回,因为它们无法跨进程边界进行通信。

了解启动模式

可能是因为您在launchHomeScreen()函数中使用了finish()方法

我的回复很晚,但它可能会帮助某人。 在我的情况下,我将 android:noHistory 设置为 true,这导致活动堆栈被擦除,并且 onActivityResult 回调没有被触发,而是应用程序正在刷新。 只需从 AndroidManifest 中的活动中删除该属性即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM