title: Google sign in fragment , android?
body: <p>Using android studio</p>
  
  <p>I am trying to sign in from a fragment</p>
  
  <p>Activity Code:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      if(savedInstanceState==null)
      {
          Fragment fragment = null;
          fragment=new LoginFragment();
          if (fragment != null) {
              FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction()
                      .replace(R.id.frame_container, fragment).commit();
  
              // update selected item and title, then close the drawer
          } else {
              // error in creating fragment
              Log.e("MainActivity", "Error in creating fragment");
          }
  
      }
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  
      if (requestCode == LoginFragment.RC_SIGN_IN) {
          LoginFragment fragment = (LoginFragment) getFragmentManager()
                  .findFragmentById(R.id.frameLayoutLogin);
          fragment.onActivityResult(requestCode, resultCode, data);
      } else {
          super.onActivityResult(requestCode, resultCode, data);
      }
  }
  }
  </code></pre>
  
  <p>Fragment code:</p>
  
  <pre><code>public class LoginFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
  
  /* Request code used to invoke sign in user interactions. */
  public static final int RC_SIGN_IN = 0;
  
  /* Client used to interact with Google APIs. */
  private GoogleApiClient mGoogleApiClient;
  
  /* A flag indicating that a PendingIntent is in progress and prevents
   * us from starting further intents.
   */
  private boolean mIntentInProgress;
  
  /**
   * True if the sign-in button was clicked.  When true, we know to resolve all
   * issues preventing sign-in without waiting.
   */
  private boolean mSignInClicked;
  
  
  public LoginFragment() {
      // Required empty public constructor
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      View v = inflater.inflate(R.layout.fragment_login, container, false);
      v.findViewById(R.id.sign_in_button).setOnClickListener(this);
      mGoogleApiClient = new GoogleApiClient.Builder(((MainActivity)getActivity()))
              .addConnectionCallbacks(this)
              .addOnConnectionFailedListener(this)
              .addApi(Plus.API)
              .addScope(Plus.SCOPE_PLUS_LOGIN)
              .build();
      return v;
  }
  
  @Override
  public void onStart() {
      super.onStart();
      mGoogleApiClient.connect();
  }
  
  @Override
  public void onResume() {
      super.onResume();
      getActivity().setTitle("Login");
  }
  
  @Override
  public void onStop() {
      super.onStop();
      if (mGoogleApiClient.isConnected()) {
          mGoogleApiClient.disconnect();
      }
  }
  
  @Override
  public void onConnected(Bundle bundle) {
      mSignInClicked = false;
      Toast.makeText(getActivity(), "User is connected!", Toast.LENGTH_LONG).show();
  }
  
  @Override
  public void onConnectionSuspended(int i) {
      mGoogleApiClient.connect();
  }
  
  @Override
  public void onConnectionFailed(ConnectionResult connectionResult) {
      if (!mIntentInProgress) {
          if (mSignInClicked &amp;&amp; connectionResult.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 {
                  connectionResult.startResolutionForResult(((MainActivity)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 onActivityResult(int requestCode, int resultCode, Intent data) {
   //        super.onActivityResult(requestCode, resultCode, data);
      if (requestCode == RC_SIGN_IN) {
          if (resultCode != MainActivity.RESULT_OK) {
              mSignInClicked = false;
          }
  
          mIntentInProgress = false;
  
          if (!mGoogleApiClient.isConnected()) {
              mGoogleApiClient.reconnect();
          }
      }
  }
  
  @Override
  public void onClick(View v) {
      if (v.getId() == R.id.sign_in_button &amp;&amp; !mGoogleApiClient.isConnecting()) {
          mSignInClicked = true;
          mGoogleApiClient.connect();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I getting null pointer exception when I select the email:</p>
  
  <pre><code>java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.rnit/com.rnit.MainActivity}: java.lang.NullPointerException
          at android.app.ActivityThread.deliverResults(ActivityThread.java:3447)
          at android.app.ActivityThread.handleSendResult(ActivityThread.java:3490)
          at android.app.ActivityThread.access$1300(ActivityThread.java:145)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1291)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:136)
          at android.app.ActivityThread.main(ActivityThread.java:5127)
          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:825)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
          at dalvik.system.NativeStart.main(Native Method)
   Caused by: java.lang.NullPointerException
          at com.rnit.MainActivity.onActivityResult(MainActivity.java:45)
          at android.app.Activity.dispatchActivityResult(Activity.java:5423)
          at android.app.ActivityThread.deliverResults(ActivityThread.java:3443)
  </code></pre>
  
  <p>               </p>
  
  <p>Line no 45 is</p>
  
  <pre><code>            fragment.onActivityResult(requestCode, resultCode, data);
  </code></pre>
  
  <p>What is the issue here? Any help?</p>