我一直在尝试为我的应用创建一个登录页面,并决定使用G +作为登录方式之一。
我的代码如下
public class LoginActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {
String Name,Pass;
private static final int RC_SIGN_IN = 0;
private static final String TAG = "MainActivity";
private GoogleApiClient mGoogleApiClient;
private boolean mIntentInProgress;
private boolean mSignInClicked;
private ConnectionResult mConnectionResult;
SharedPreferences preferences;
SharedPreferences.Editor edit;
SignInButton b3;
final String App_id="WP74ib9AoAQtnKbJxgz5FkMSQLDq1raIIVyqIqup",C_key="FUXJl7JcyGP7cJuktrdoBr8WtnpzjpIQ9UK1UWc6";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
b3=(SignInButton)findViewById(R.id.button3);
mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Plus.API)
.addConnectionCallbacks(this).addOnConnectionFailedListener(this)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click();
}
});
}
public void click(){
signInWithGplus();
Toast.makeText(getApplicationContext(),"Inside click",Toast.LENGTH_SHORT).show();
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
public void onConnectionFailed(ConnectionResult result) {
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
resolveSignInError();
}
}
}
@Override
protected void onActivityResult(int requestCode, int responseCode,
Intent intent) {
if (requestCode == RC_SIGN_IN) {
if (responseCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnected(Bundle arg0) {
mSignInClicked = false;
getProfileInformation();
}
@Override
public void onConnectionSuspended(int arg0) {
mGoogleApiClient.connect();
}
private void signInWithGplus() {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
else
getProfileInformation();
}
/**
* Method to resolve any signin errors
* */
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
} catch (IntentSender.SendIntentException e) {
Toast.makeText(getApplicationContext(),
"SignIn Error", Toast.LENGTH_LONG).show();
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
/**
* Fetching user's information name, email, profile pic
* */
private void getProfileInformation() {
try {
//Toast.makeText(getApplicationContext(), "Getting Profile",Toast.LENGTH_SHORT).show();
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi
.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String personPhotoUrl = currentPerson.getImage().getUrl();
String personGooglePlusProfile = currentPerson.getUrl();
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
Toast.makeText(getApplicationContext(),"Got Information",Toast.LENGTH_SHORT).show();
Log.e(TAG, "Name: " + personName + ", plusProfile: "
+ personGooglePlusProfile + ", email: " + email
+ ", Image: " + personPhotoUrl);
} else {
//Toast.makeText(getApplicationContext(),
// "Person information is null", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
// Toast.makeText(getApplicationContext(),
// "Person information is null", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
//if (id == R.id.next) {
// return true;
//}
return super.onOptionsItemSelected(item);
} }
我已经实现了获取数据所需的所有必需功能。
当我启动应用程序并单击登录按钮时,我在
处获得空指针异常 if (mConnectionResult.hasResolution())
错误日志如下所示
java.lang.NullPointerException
at com.example.nirmal.playreskey.LoginActivity.resolveSignInError(LoginActivity.java:161)
at com.example.nirmal.playreskey.LoginActivity.signInWithGplus(LoginActivity.java:151)
at com.example.nirmal.playreskey.LoginActivity.click(LoginActivity.java:92)
at com.example.nirmal.playreskey.LoginActivity$3.onClick(LoginActivity.java:87)
at com.google.android.gms.common.SignInButton.onClick(Unknown Source)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
我搜索了这种类型的错误,发现如果我们尝试在执行onStart()
之前调用signin函数,就会发生这种情况。所以我创建了一个toast来检查onStart()
是否被执行然后单击按钮然后我也得到了同样的错误。
此错误的原因是什么?以及如何纠正它?
答案 0 :(得分:0)
我遵循了androidhive的教程。这几乎是同一个教程
private void resolveSignInError() {
if (mConnectionResult != null) { //Added null check
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
} catch (IntentSender.SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
}
您可以使用它来处理该方法中的null异常。