2017-09-03 2 views
-4

J'ai une activité qui reçoit des chaînes et des flottants (que j'ai convertis en chaînes) pour remplir certains champs de texte. Après ce processus, je dois prendre ces valeurs et les convertir en doubles pour les faire fonctionner avec la fonction "beta" et les passer à l'activité suivante, mais quelque chose ne va pas. Le programme plante quand j'essaie de démarrer l'activité "ConferenciaDosDados", mais cela fonctionnait bien avant de créer le bouton "next" et la fonction "beta". Qu'est-ce que je fais mal?La fonction ne fonctionne pas et fait planter le programme lorsque je démarre l'activité

Activité "ConferenciaDosDados":

package com.mateus.ligacoestubulares; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

import java.math.BigDecimal; 

public class ConferenciaDosDados extends AppCompatActivity { 

EditText normal1; 
EditText normal0; 
EditText momento; 
EditText teta; 
EditText Fy1; 
EditText Fyo; 
EditText T1; 
EditText To; 
EditText BaseM; 
EditText AlturaM; 
EditText BaseB; 
EditText AlturaB; 
Button next; 
Intent intentNext; 

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

    normal1 = (EditText) findViewById(R.id.NormalM); 
    normal0 = (EditText) findViewById(R.id.NormalB); 
    momento = (EditText) findViewById(R.id.Momento); 
    teta = (EditText) findViewById(R.id.Teta1); 
    Fy1 = (EditText) findViewById(R.id.fy1); 
    Fyo = (EditText) findViewById(R.id.fyo); 
    T1 = (EditText) findViewById(R.id.t1); 
    To = (EditText) findViewById(R.id.to); 
    BaseM = (EditText) findViewById(R.id.b1); 
    AlturaM = (EditText) findViewById(R.id.h1); 
    BaseB = (EditText) findViewById(R.id.bo); 
    AlturaB = (EditText) findViewById(R.id.ho); 

    String normalM = getIntent().getExtras().getString("NormalM"); 
    String normalB = getIntent().getExtras().getString("NormalB"); 
    String momentoB = getIntent().getExtras().getString("MomentoB"); 
    String teta1 = getIntent().getExtras().getString("Teta1"); 
    String fy1 = getIntent().getExtras().getString("fy1"); 
    String fyo = getIntent().getExtras().getString("fyo"); 
    String t1 = getIntent().getExtras().getString("t1"); 
    String to = getIntent().getExtras().getString("to"); 

    Float baseMt = getIntent().getExtras().getFloat("baseM"); 
    String b1 = baseMt.toString(); 
    Float alturaMt = getIntent().getExtras().getFloat("alturaM"); 
    String h1 = alturaMt.toString(); 
    Float baseBz = getIntent().getExtras().getFloat("baseB"); 
    String bo = baseBz.toString(); 
    Float alturaBz = getIntent().getExtras().getFloat("alturaB"); 
    String ho = alturaBz.toString(); 

    normal1.setText(normalM); 
    normal0.setText(normalB); 
    momento.setText(momentoB); 
    teta.setText(teta1); 
    Fy1.setText(fy1); 
    Fyo.setText(fyo); 
    T1.setText(t1); 
    To.setText(to); 
    BaseM.setText(b1); 
    AlturaM.setText(h1); 
    BaseB.setText(bo); 
    AlturaB.setText(ho); 

    next = (Button) findViewById(R.id.prosseguir); 
    intentNext = new Intent(ConferenciaDosDados.this, Resultados.class); 

    next.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      double hM = Double.parseDouble(AlturaM.getText().toString()); 
      double hB = Double.parseDouble(AlturaB.getText().toString()); 

      String valorBeta = Double.toString(beta(hM, hB)); 

      intentNext.putExtra("beta", valorBeta); 
      startActivity(intentNext); 

     } 
    }); 

} 

public double beta(double h1, double h2){ 

    double b = (h1/h2); 

    b = Math.round(b*100.0)/100.0; 

    return b; 
} 
} 

Logcat:

--------- beginning of crash 
09-03 22:00:48.299 2752-2752/com.mateus.ligacoestubulares E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.mateus.ligacoestubulares, PID: 2752 
                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mateus.ligacoestubulares/com.mateus.ligacoestubulares.ConferenciaDosDados}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 
                      at com.mateus.ligacoestubulares.ConferenciaDosDados.onCreate(ConferenciaDosDados.java:86) 
                      at android.app.Activity.performCreate(Activity.java:6237) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)  
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
                      at android.os.Handler.dispatchMessage(Handler.java:102)  
                      at android.os.Looper.loop(Looper.java:148)  
                      at android.app.ActivityThread.main(ActivityThread.java:5417)  
                      at java.lang.reflect.Method.invoke(Native Method)  
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
+1

Afficher vos logcats plz –

+1

Bienvenue dans Stack Overflow! Vous avez posté beaucoup de code dans votre question, ce qui nous empêche de savoir exactement où se situe le problème (et aux futurs lecteurs). Veuillez réduire votre code de problème à 10 lignes ou moins. Voir: [Comment créer un exemple minimal, complet et vérifiable] (http://stackoverflow.com/help/mcve) et [Comment déboguer de petits programmes] (https://ericlippert.com/2014/03/05/how-to-debug-petits-programmes /). –

Répondre

1

variable next est null lorsque vous appelez next.setOnClickListener() probablement R.id.prosseguir est un mauvais identifiant

0

Il est une erreur se produit dans la cartographie du bouton next. Sur cette ligne:

next = (Button) findViewById(R.id.prosseguir); 

Probablement R.id.prosseguir est pas id bouton correct. Alors, allez dans votre xml activity_conferencia_dos_dados et assurez-vous que l'ID du bouton est correct

+0

C'était tout, merci. J'ai copié le code d'une autre activité et j'ai oublié de changer l'identifiant du bouton. Désolé pour mon insouciance. –