2015-06-09 5 views
5

मैं अपने एंड्रॉइड ऐप में वर्किंग फाइन में Google प्लस लॉगिन का उपयोग कर रहा हूं।एंड्रॉइड में काम नहीं कर रहे Google प्लस अकाउंट से साइन आउट, नल पॉइंटर अपवाद

किसी अन्य उद्देश्य से लॉगआउट बटन पर क्लिक करते समय नल पॉइंटर अपवाद दिखा रहा है।

भी लॉगिन बटन पर क्लिक करें, सीधे मेरे अगले पृष्ठ पर जा रहा है। साइन इन पेज में नहीं।

लेकिन डेमो एप्लिकेशन से लॉगआउट यह ठीक काम कर रहा है।

मैंने संबंधित उत्तरों की खोज की है, कोई समाधान अभी भी एक ही नहीं है।

मदद मुझे हल करने में यह

link 1

link 2

मैं 2 लिंक के लिए भेजा लेकिन अगले मंशा भी दिखाई दे रहा नल पॉइंटर एक्सेप्शन से साइन आउट कर दिया है।

मेरे लॉगआउट कोड:

public void googlePlusLogout() 
    { 
     if (mGoogleApiClient.isConnected()) 
     { 
      Plus.AccountApi.clearDefaultAccount(mGoogleApiClient); 
      mGoogleApiClient.disconnect(); 
      mGoogleApiClient.connect(); 
      updateProfile(false); 
     } 

    } 
यहाँ

साइन इन वर्ग के लिए अन्य इरादे से विधि बुला

otherintent.java

    session.logoutUser();// here i am clearing the shared pref 
        sign_in Signinclass = new sign_in(); 
        Signinclass.googlePlusLogout(); 
        finish(); 

यह मेरा डेमो कोड अपनी कार्यशील ठीक एक ही कोड रहा है मेरे एप्लिकेशन लॉग इन में ठीक काम कर रहा है, लेकिन किसी अन्य पेज से लॉगआउट कॉलिंग काम नहीं कर रहा है। शून्य सूचक अपवाद दिखा रहा है।

AndroidGooglePlusExample जावा

public class AndroidGooglePlusExample extends Activity implements OnClickListener, ConnectionCallbacks, OnConnectionFailedListener { 

    private static final int RC_SIGN_IN = 0; 

    // Google client to communicate with Google 
    private GoogleApiClient mGoogleApiClient; 

    private boolean mIntentInProgress; 
    private boolean signedInUser; 
    private ConnectionResult mConnectionResult; 
    private SignInButton signinButton; 
    private ImageView image; 
    private TextView username, emailLabel; 
    private LinearLayout profileFrame, signinFrame; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     signinButton = (SignInButton) findViewById(R.id.signin); 
     signinButton.setOnClickListener(this); 

     image = (ImageView) findViewById(R.id.image); 
     username = (TextView) findViewById(R.id.username); 
     emailLabel = (TextView) findViewById(R.id.email); 

     profileFrame = (LinearLayout) findViewById(R.id.profileFrame); 
     signinFrame = (LinearLayout) findViewById(R.id.signinFrame); 

     mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API, Plus.PlusOptions.builder().build()).addScope(Plus.SCOPE_PLUS_LOGIN).build(); 
    } 

    protected void onStart() { 
     super.onStart(); 
     mGoogleApiClient.connect(); 
    } 

    protected void onStop() { 
     super.onStop(); 
     if (mGoogleApiClient.isConnected()) { 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    private void resolveSignInError() { 
     if (mConnectionResult.hasResolution()) { 
      try { 
       mIntentInProgress = true; 
       mConnectionResult.startResolutionForResult(this, RC_SIGN_IN); 
      } catch (SendIntentException e) { 
       mIntentInProgress = false; 
       mGoogleApiClient.connect(); 
      } 
     } 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     if (!result.hasResolution()) { 
      GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show(); 
      return; 
     } 

     if (!mIntentInProgress) { 
      // store mConnectionResult 
      mConnectionResult = result; 

      if (signedInUser) { 
       resolveSignInError(); 
      } 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) { 
     switch (requestCode) { 
     case RC_SIGN_IN: 
      if (responseCode == RESULT_OK) { 
       signedInUser = false; 

      } 
      mIntentInProgress = false; 
      if (!mGoogleApiClient.isConnecting()) { 
       mGoogleApiClient.connect(); 
      } 
      break; 
     } 
    } 

    @Override 
    public void onConnected(Bundle arg0) { 
     signedInUser = false; 
     Toast.makeText(this, "Connected", Toast.LENGTH_LONG).show(); 
     getProfileInformation(); 
    } 

    private void updateProfile(boolean isSignedIn) { 
     if (isSignedIn) { 
      signinFrame.setVisibility(View.GONE); 
      profileFrame.setVisibility(View.VISIBLE); 

     } else { 
      signinFrame.setVisibility(View.VISIBLE); 
      profileFrame.setVisibility(View.GONE); 
     } 
    } 

    private void getProfileInformation() { 
     try { 
      if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) { 
       Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient); 
       String personName = currentPerson.getDisplayName(); 
       String personPhotoUrl = currentPerson.getImage().getUrl(); 
       String email = Plus.AccountApi.getAccountName(mGoogleApiClient); 

       username.setText(personName); 
       emailLabel.setText(email); 

       new LoadProfileImage(image).execute(personPhotoUrl); 

       // update profile frame with new info about Google Account 
       // profile 
       updateProfile(true); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int cause) { 
     mGoogleApiClient.connect(); 
     updateProfile(false); 
    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.signin: 
      googlePlusLogin(); 
      break; 
     } 
    } 

    public void signIn(View v) { 
     googlePlusLogin(); 
    } 

    public void logout(View v) { 
     googlePlusLogout(); 
    } 

    private void googlePlusLogin() { 
     if (!mGoogleApiClient.isConnecting()) { 
      signedInUser = true; 
      resolveSignInError(); 
     } 
    } 

    private void googlePlusLogout() { 
     if (mGoogleApiClient.isConnected()) { 
      Plus.AccountApi.clearDefaultAccount(mGoogleApiClient); 
      mGoogleApiClient.disconnect(); 
      mGoogleApiClient.connect(); 
      updateProfile(false); 
     } 
    } 

    // download Google Account profile image, to complete profile 
    private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> { 
     ImageView downloadedImage; 

     public LoadProfileImage(ImageView image) { 
      this.downloadedImage = image; 
     } 

     protected Bitmap doInBackground(String... urls) { 
      String url = urls[0]; 
      Bitmap icon = null; 
      try { 
       InputStream in = new java.net.URL(url).openStream(); 
       icon = BitmapFactory.decodeStream(in); 
      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return icon; 
     } 

     protected void onPostExecute(Bitmap result) { 
      downloadedImage.setImageBitmap(result); 
     } 
    } 
+0

इस कोड ..this मेरे लिए काम कर रहा है काम करना चाहिए कि आप कुछ और अधिक जानकारी पोस्ट ..can तो हम जांच कर सकते हैं, जहां आप गलती कर रहे हैं – Tufan

+0

मैं इस विधि को अन्य इरादे से कॉल कर रहा हूं, साइन इन पेज में नहीं। लेकिन मैंने मुख्य गतिविधि से डेमो ऐप साइन आउट करने का प्रयास किया है, यह ठीक काम कर रहा है – Kumar

+0

यह काम कर सकता है .. आप दूसरे पेज में लॉगआउट क्यों नहीं कर सकते हैं .. आप वास्तव में mGoogleApiClient के लिए शून्य पोंटर अपवाद प्राप्त करेंगे ... लॉग इन करने का प्रयास करें जब आप लॉगिन करते हैं gplus nd साझा मूल्यों में अपने मानों को सहेजें, यह मेरे लिए काम किया – Tufan

उत्तर

2

मैं अपने प्रश्न

अपने लॉगआउट गतिविधि पृष्ठ में इस संहिता जोड़े के लिए समाधान मिल गया। इसका काम करता है पूरी तरह से

मेरे लॉगआउट पेज गतिविधि

public class LogoutActivity extends Activity implements OnClickListener, 
     ConnectionCallbacks, OnConnectionFailedListener, 
     ResultCallback<People.LoadPeopleResult> { 

     GoogleApiClient mGoogleApiClient; 
     boolean mSignInClicked; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this).addApi(Plus.API) 
       .addScope(Plus.SCOPE_PLUS_LOGIN).build(); 

     //copy this code on "Logout" Onclick 
     logout.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if (mGoogleApiClient.isConnected()) { 
       Plus.AccountApi.clearDefaultAccount(mGoogleApiClient); 
       mGoogleApiClient.disconnect(); 
       mGoogleApiClient.connect(); 
       // updateUI(false); 
       System.err.println("LOG OUT ^^^^^^^^^^^^^^^^^^^^ SUCESS"); 
      } 

      } 
     }); 

    } 
    @Override 
    public void onConnected(Bundle arg0) { 
     // TODO Auto-generated method stub 
     mSignInClicked = false; 

     // updateUI(true); 
     Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(
       this); 
    } 

    @Override 
    public void onConnectionSuspended(int arg0) { 
     // TODO Auto-generated method stub 
     mGoogleApiClient.connect(); 
     // updateUI(false); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult arg0) { 
     // TODO Auto-generated method stub 

    } 

    protected void onStart() { 
     super.onStart(); 
     mGoogleApiClient.connect(); 
    } 

    protected void onStop() { 
     super.onStop(); 
     if (mGoogleApiClient.isConnected()) { 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    @Override 
    public void onResult(LoadPeopleResult arg0) { 
     // TODO Auto-generated method stub 

    } 

link for reference

संबंधित मुद्दे