2017-09-06 3 views
0

MainActivity.java est la classe de réception.Null Pointer Erreur lors de la capture des données de l'intention

Signup.java est la classe Sender.

Avoir Erreur lors de la réception des données de l'intention.

MainActivity.java - classe de réception

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 

    // Class Variables 
     private String name; 
     private String email; 
     private String img_url ; 

     // Controls 
     private TextView Email; 
     private TextView Name; 
     private ImageView prof_Pic; 

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

      Name = (TextView) findViewById(R.id.profile_Name); 
      Email = (TextView) findViewById(R.id.profile_Email); 
      prof_Pic = (ImageView) findViewById(R.id.profile_Picture); 

      //Bundle extras = new Bundle(); 
      //extras.getExtras() 
      //getIntent().getExtras(); 
      Intent intent = getIntent(); 
      Bundle extra = intent.getBundleExtra("gProfileRecord"); 
      if (extra != null) { 
       String email= extra.getString("email"); 
       Email.setText(email); **/* *** ERROR *** */** 
       String name = extra.getString("firstName"); 
       Name.setText(name); 
       String img_url = extra.getString("profile_Pic"); 
       Glide.with(this).load(img_url).into(prof_Pic); 
      }} 

SignUp.java - Classe Sender

public class Signup extends AppCompatActivity 
       implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener 
     { 
      // Variables for controls 
      private LinearLayout prof_layout; 
      private Button SignOut; 
      private TextView Name, Email, GivenName, FamilyName, PersonId; 
      private ImageView prof_pic; 

      // Some class variables 
      private String name; 
      private String email; 
      private String personGivenName; 
      private String personFamilyName; 
      private String personId; 
      private String img_url ; 
      // variables for Google SignIN process 

      private SignInButton SignIn; 
      private GoogleApiClient mGoogleApiClient; 
      private static final int REQ_CODE = 9001; 

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

       prof_layout = (LinearLayout) findViewById(R.id.profile_layout); 
       SignOut = (Button) findViewById(R.id.bn_logout); 
       SignIn = (SignInButton)findViewById(R.id.sign_in_button); 
       Name = (TextView) findViewById(R.id.name); 
       Email = (TextView) findViewById(R.id.email); 
       GivenName = (TextView) findViewById(R.id.given_Name); 
       FamilyName = (TextView) findViewById(R.id.family_name); 
       PersonId = (TextView) findViewById(R.id.person_id); 
       prof_pic = (ImageView) findViewById(R.id.profile_Pic); 

       // Event Listener 
       SignIn.setOnClickListener(this); 
       SignOut.setOnClickListener(this); 
       prof_layout.setVisibility(View.GONE); 

       GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
         .requestEmail() 
         .build(); 
       mGoogleApiClient = new GoogleApiClient.Builder(this) 
         .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) 
         .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
         .build(); 

      } 

      @Override 
      public void onClick(View v) { 

       switch(v.getId()){ 
        case R.id.sign_in_button: 
         signIn(); 
         break; 
        case R.id.bn_logout: 
         signOut(); 
         break; 
       } 

      } 

      @Override 
      public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

      } 


      // Needed Methods 
      private void signIn(){ 
       Intent intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
       startActivityForResult(intent, REQ_CODE); 
      } 

      private void signOut() { 
       Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(new ResultCallback<Status>() { 
        @Override 
        public void onResult(@NonNull Status status) { 
         updateUI(false); 
        } 
       }); 
      } 

      private void handleResult(GoogleSignInResult result){ 
       if(result.isSuccess()){ 
        GoogleSignInAccount account = result.getSignInAccount(); 
        if (account != null) { 
         // getting the data 
        // name = account.getDisplayName(); 
        email = account.getEmail(); 
        personGivenName = account.getGivenName(); 
        personFamilyName = account.getFamilyName(); 
        // personId = account.getId(); 
        img_url = account.getPhotoUrl().toString(); 

         // setting the data 
         /* Name.setText(name); 
         Email.setText(email); 
         GivenName.setText(personGivenName); 
         FamilyName.setText(personFamilyName); 
         PersonId.setText(personId); 
         Glide.with(this).load(img_url).into(prof_pic);*/ 
         updateUI(true); 
        } 

       }else{ 
        updateUI(false); 
       } 
      } 

      private void updateUI(boolean isLogin){ 
       if(isLogin){ 
        prof_layout.setVisibility(View.VISIBLE); 
        SignIn.setVisibility(View.GONE); 

        Toast.makeText(Signup.this, "Success !", 
          Toast.LENGTH_LONG).show(); 

        Intent intent = new Intent(this, MainActivity.class); 
        Bundle extras = new Bundle(); 
         extras.putString("email",email); 
         extras.putString("firstName",personGivenName); 
         // extras.putString("lastName",personFamilyName); 
         extras.putString("profile_Pic",img_url); 
        intent.putExtra("gProfileRecord",extras); 
        startActivity(intent); 

       }else { 
        Toast.makeText(Signup.this, "Failed !", 
          Toast.LENGTH_LONG).show(); 
        SignIn.setVisibility(View.VISIBLE); 
        prof_layout.setVisibility(View.GONE); 
       } 
      } 

      @Override 
      protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
       super.onActivityResult(requestCode,resultCode,data); 

       if(requestCode == REQ_CODE){ 
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
        handleResult(result); 
       } 
      } 
     } 

Erreur

Erreur fatale: principale Pro cess: com.example.yash.lifecatalog_beatprocrastination, PID: 12006 java.lang.RuntimeException: Impossible de démarrer l'activité ComponentInfo {com.example.yash.lifecatalog_beatprocrastination/com.example.yash.lifecatalog_beatprocrastination.MainActivity}: java.lang. NullPointerException: Tentative d'invocation de la méthode virtuelle 'void android.widget.TextView.setText (java.lang.CharSequence)' sur une référence d'objet null at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2817) at android. app.ActivityThread.handleLaunchActivity (ActivityThread.java:2892) at android.app.ActivityThread.-wrap11 (Unknown Source: 0) à l'adresse android.app.ActivityThread $ H.handle Message (ActivityThread.java:1593) à android.os.Handler.dispatchMessage (Handler.java:105) à android.os.Looper.loop (Looper.java:164) à android.app.ActivityThread.main (ActivityThread.java:6541) à java.lang.reflect.Method.invoke (méthode native) à com.android.internal.os.Zygote $ MethodAndArgsCaller.run (Zygote.java:240) à com.android.internal .os.ZygoteInit.main (ZygoteInit.java:767) Causé par:java.lang.NullPointerException: Tentative d'invocation de la méthode virtuelle 'void android.widget.TextView.setText (java. lang.CharSequence) » sur une référence d'objet null à com.example.yash.lifecatalog_beatprocrastination.MainActivity.onCreate (MainActivity.java:50) à android.app.Activity.performCreate (Activity.java:6975) à androïde .app.Instrumentation.callActivityOnCreate (Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2770) à l'adresse android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2892) at android.app .ActivityThread.-wrap11 (Unknown Source: 0) à l'adresse android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1593) à android.os.Handler.dispatchMessage (Handler.java:105) à android.os.Looper.loop (Looper.java:164) à android.app.ActivityThread.main (ActivityThread.java:6541) à java.lang.reflect.Method.invoquer (Native Method) à com.android.internal.os.Zygote $ MethodAndArgsCaller.run (Zygote.java:240) à com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767)

Répondre

2

vous devez utiliser startActivityForResult(intent); pour obtenir les données au onActivityResult

+0

A travaillé comme un charme! –

+0

Le plus Bienvenue :) –

+1

Acceptez-le comme une réponse Oui – Orvenito

1

Stacktrace vous dit Email référence est nul (vous pouvez facilement le débogage et la sortie de cette valeur pour le vérifier). Je suppose que quelque chose ne va pas dans votre mise en page xml, par exemple, assurez-vous que vous n'utilisez pas les mêmes identifiants dans _activity_main_ et _activity_signup_.