主要问题在于onActivityResult()方法。它无法将结果发送回片段。它会生成nullpointerexception。
MainActivity Class
package com.example.kunalgoyal.login;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment4 f=(Fragment4)getSupportFragmentManager().findFragmentById(R.id.fragment);
if(f !=null)
{
f.onActivityResult(requestCode,resultCode,data);
}
}
}
片段类
package com.example.kunalgoyal.login;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.plus.People;
import com.google.android.gms.plus.Plus;
import com.google.android.gms.plus.model.people.Person;
public class Fragment4 extends android.support.v4.app.Fragment implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener, ResultCallback<People.LoadPeopleResult> { private GoogleApiClient mGoogleApiClient;
private static final int RC_SIGN_IN = 0;
private boolean mIntentInProgress;
private boolean mSignInClicked;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(getActivity().getApplicationContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(new Scope("profile"))
.build();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment4, container,false);
v.findViewById(R.id.sign_in_button).setOnClickListener(this);
return v;
}
@Override
public void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
public void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
}
@Override
public void onConnected(Bundle bundle) {
mSignInClicked = false;
Log.i("onConnected", "aaaaaaaaaaaaaaa");
Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
Person.Image personPhoto = currentPerson.getImage();
String personGooglePlusProfile = currentPerson.getUrl();
Toast.makeText(getActivity().getApplicationContext(), " "+ personName + ", ", Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
Log.i("on Suspended", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.sign_in_button && !mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
mGoogleApiClient.connect();
Log.i("on Click", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
}
}
@Override
public void onActivityResult(int requestCode, int responseCode, Intent intent) {
if (requestCode == RC_SIGN_IN) {
if (responseCode != Activity.RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.reconnect();
}
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!mIntentInProgress) {
if (mSignInClicked && result.hasResolution()) {
// The user has already clicked 'sign-in' so we attempt to resolve all
// errors until the user is signed in, or they cancel.
try {
result.startResolutionForResult(getActivity(), RC_SIGN_IN);
mIntentInProgress = true;
} catch (IntentSender.SendIntentException e) {
// The intent was canceled before it was sent. Return to the default
// state and attempt to connect to get an updated ConnectionResult.
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
}
@Override
public void onResult(People.LoadPeopleResult peopleDaa) {
}
}
activity_main.xml中
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.example.kunalgoyal.login.Fragment4"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
fragment_fragment4.xml
<FrameLayout 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" tools:context="com.example.kunalgoyal.login.Fragment4"
android:id="@+id/fragment">
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="150dp"
android:layout_height="90dp" />
</FrameLayout>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kunalgoyal.login" >
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
在我的应用中实施Google plus登录时遇到同样的问题 最后一个解决方案对我来说很好, 将 GoogleApiClient 对象设为静态对象。
希望它有所帮助。