scope - Google+ Sign In Button not functioning Android Studio -
hi integrating google sign in app. lately has been concern reason android studio not recognizing plus_login scope (which necessary app access basic profile info, etc). current error this
error:(40, 17) error: method addscope in class builder cannot applied given types; required: scope found: string reason: actual argument string cannot converted scope method invocation conversion
and here oncreate file error found
edit 1 - added import statement , variable declarations
package com.instigate.aggregator06; import android.content.intent; import android.content.intentsender; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.util.log; import android.view.view; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.common.api.googleapiclient.connectioncallbacks; import com.google.android.gms.common.api.googleapiclient.onconnectionfailedlistener; import com.google.android.gms.common.scopes; import com.google.android.gms.common.api.scope; import com.google.android.gms.plus.plus; public class socialnetworkactivity extends appcompatactivity implements connectioncallbacks, onconnectionfailedlistener, view.onclicklistener { private static final string tag = "social network activity"; /* request code used invoke sign in user interactions. */ private static final int rc_sign_in = 0; /* client used interact google apis. */ private googleapiclient mgoogleapiclient; /* flag indicating pendingintent in progress , prevents * starting further intents. */ private boolean mintentinprogress; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mgoogleapiclient = new googleapiclient.builder(this) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .addapi(plus.api) .addscope(scopes.plus_login) // <----- error found .addscope(scopes.plus_me) .build(); findviewbyid(r.id.sign_in_button).setonclicklistener(this); }
i have searched while find solution error (seeing plus_login scope should functioning) yet have found no solution.
edit 2 - errors after solving found answer this. apparently instead of:
.addscope(scopes.plus_login) .addscope(scopes.plus_me)
we write this
.addscope(new scope(scopes.plus_login)) .addscope(new scope(scopes.plus_me))
which solved problem.
however, revealed new problem:
findviewbyid(r.id.sign_in_button).setonclicklistener(this);
where logcat points out there nullpointerexception @ point.
edit 3 - solved nullpointerexception have solved nullpointer exception problem, google+ sign in button still not function.
here overall class (continuation of code snippet mentioned above)
package com.instigate.aggregator06; import android.content.intent; import android.content.intentsender; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.util.log; import android.view.view; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.common.api.googleapiclient.connectioncallbacks; import com.google.android.gms.common.api.googleapiclient.onconnectionfailedlistener; import com.google.android.gms.common.scopes; import com.google.android.gms.common.api.scope; import com.google.android.gms.plus.plus; public class socialnetworkactivity extends appcompatactivity implements connectioncallbacks, onconnectionfailedlistener, view.onclicklistener { private static final string tag = "social network activity"; /* request code used invoke sign in user interactions. */ private static final int rc_sign_in = 0; /* client used interact google apis. */ private googleapiclient mgoogleapiclient; /* flag indicating pendingintent in progress , prevents * starting further intents. */ private boolean mintentinprogress; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_social_network); mgoogleapiclient = new googleapiclient.builder(this) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .addapi(plus.api) //.addscope(scopes.plus_login) //.addscope(scopes.plus_me) .addscope(new scope(scopes.plus_me)) .addscope(new scope(scopes.plus_login)) .build(); this.findviewbyid(r.id.sign_in_button).setonclicklistener(this); } protected void onstart() { super.onstart(); mgoogleapiclient.connect(); } protected void onstop() { super.onstop(); if (mgoogleapiclient.isconnected()) { mgoogleapiclient.disconnect(); } } /* code in case second 1 doesn't work @override public void onconnectionfailed(connectionresult connectionresult) { // not connect google play services. user needs select account, // grant permissions or resolve error in order sign in. refer javadoc // connectionresult see possible error codes. log.d(tag, "onconnectionfailed:" + connectionresult); if (!misresolving && mshouldresolve) { if (connectionresult.hasresolution()) { try { connectionresult.startresolutionforresult(this, rc_sign_in); misresolving = true; } catch (intentsender.sendintentexception e) { log.e(tag, "could not resolve connectionresult.", e); misresolving = false; mgoogleapiclient.connect(); } } else { // not resolve connection result, show user // error dialog. showerrordialog(connectionresult); } } }*/ public void onconnectionfailed(connectionresult result) { if (!mintentinprogress && result.hasresolution()) { try { mintentinprogress = true; startintentsenderforresult(result.getresolution().getintentsender(), rc_sign_in, null, 0, 0, 0); } catch (intentsender.sendintentexception e) { // intent canceled before sent. return default // state , attempt connect updated connectionresult. mintentinprogress = false; mgoogleapiclient.connect(); } } } public void onconnected(bundle connectionhint) { // we've resolved connection errors. mgoogleapiclient can used // access google apis on behalf of user. log.d(tag, "onconnected:" + connectionhint); intent j = new intent(socialnetworkactivity.this, accountpageactivity.class); startactivity(j); } protected void onactivityresult(int requestcode, int responsecode, intent intent) { if (requestcode == rc_sign_in) { mintentinprogress = false; if (!mgoogleapiclient.isconnecting()) { mgoogleapiclient.connect(); } } } public void onconnectionsuspended(int cause) { mgoogleapiclient.connect(); } @override public void onclick(view v) { if (v.getid() == r.id.sign_in_button) { //onsigninclicked(); mgoogleapiclient.connect(); } // ... } /* find unnecesary private void onsigninclicked() { // user clicked sign-in button, begin sign-in process , automatically // attempt resolve errors occur. //mshouldresolve = true; mgoogleapiclient.connect(); // show message user signing in. //mstatustextview.settext(r.string.signing_in); }*/ public void backtomainpage(view view) { intent j = new intent(socialnetworkactivity.this, mainactivity.class); startactivity(j); } }
and here xml class file
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context="com.instigate.aggregator06.selectsnactivity" android:orientation="vertical" android:id="@+id/selectsnactivity" android:weightsum="1"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/backbtn2" android:id="@+id/backbtntest" android:layout_gravity="right" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_marginbottom="43dp" android:onclick="backtomainpage"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="@string/selectsocialnetwork" android:id="@+id/selectsocialnetworkheader" android:textstyle="bold" android:textsize="36sp" android:layout_gravity="center_horizontal" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" /> <com.google.android.gms.common.signinbutton android:id="@+id/sign_in_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/selectsocialnetworkheader" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_margintop="52dp" /> </relativelayout>
any appreciated, thank you.
you have
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate);
you missing setcontentview(r.layout.activity_main)
your layout must have
<com.google.android.gms.common.signinbutton android:id="@+id/sign_in_button" android:layout_width="wrap_content" android:layout_height="wrap_content" />
also check id of button in xml , make sure reference same in activity
Comments
Post a Comment