2013-09-25 2 views
1

Je fais une application simple pour faire un calcul en utilisant l'entrée de l'utilisateur mais j'ai du mal à utiliser la classe Android Math pour faire les calculs. Le compilateur me diterreur essayant de passer la variable et passer à une nouvelle activité

ne peut pas lancer de EditText doubler

que je dois faire pour utiliser les atan et pow fonctions mathématiques.

La deuxième chose dont je ne suis pas sûr est comment afficher les calculs dans mon editTexts avec les id: mark1 et mark2.

Mon MainActivity ressemble à ceci:

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

     //setting the variables to the xml id's and setting the click listener on the calc button 
     offsetLength = (EditText)findViewById(R.id.offLength); 
     offsetDepth = (EditText)findViewById(R.id.offDepth); 
     ductDepth = (EditText)findViewById(R.id.ductDepth); 
     calculate = (Button)findViewById(R.id.calc); 
     calculate.setOnClickListener((OnClickListener) this); 
    } 

    //called when button is clicked. 
    public void OnClick(View v) 
    { 

     //calculations here: 
     double tri1,tri2; 
     double marking1,marking2; 

     marking1 = pow((double)offsetLength,2) + Math.pow((double)offsetDepth,2); 
     tri1 = (float)offsetDepth/(float)offsetLength; 
     tri2 = (float)ductDepth/Math.atan((float)tri1); 
     marking2 = ductDepth/Math.atan(float)(tri2); 

     //passing the calc results to my CalcResult activity. 
     Intent myIntent = new Intent(this, CalcResult.class); 
     myIntent.putExtra("number1", marking1); 
     myIntent.putExtra("number2", marking2); 

     startActivity(myIntent); 
     Intent i = new Intent(this,CalcResult.class); 
     break; 

     } 

    } 

C'est la classe CalcResult où je "essaie" de passer les deux résultats de calcul à leurs editTexts:

public class CalcResult extends MainActivity 
{ 
    EditText res1,res2; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.result); 
     res1 = (EditText)findViewById(R.id.mark1); 
     res2 = (EditText)findViewById(R.id.mark2); 

     Intent intent = getIntent(); 
     Bundle bundle = intent.getExtras(); 
     Double mark1 = bundle.double("marking1"); 
     Double mark2 = bundle.double("marking2"); 
    } 

} 

Répondre

0

Vous avez ce

double marking1,marking2; // double 
marking1 = pow((double)offsetLength,2) + Math.pow((double)offsetDepth,2); 
Intent myIntent = new Intent(this, CalcResult.class); 
myIntent.putExtra("number1", marking1); 

Lors de la récupération, vous avez cette

String mark1 = bundle.getString("marking1"); // retrieving as string 

Modification

double mark1 = bundle.getDouble("marking1"); // similar for mark2 

Mise à jour

Vous avez cette

offsetLength = (EditText)findViewById(R.id.offLength); 

Puis

marking1 = pow((double)offsetLength,2) + Math.pow((double)offsetDepth,2); 

Vous n'obtenez même pas la valeur d'editext.

Dans le clic

String getoffsetlength = offsetLength.getText().toString(); 

Convertir la chaîne à doubler du flottant selon vos besoins

double off = Double.parseDouble(getoffsetlength); 
+0

d'accord, il devrait donc ressembler à ceci: 'deux mark1 = bundle.getDouble (» marking1 ");" mais qu'est-ce que je fais d'autre mal avec les calculs ?, car je reçois des erreurs de casting là-bas. – user2815899

+0

@ user2815899 vous devriez obtenir le texte d'editext et le convertir en un format que vous voulez – Raghunandan

+0

spécifiquement 'ne peut pas transtyper d'editText en Double. ' – user2815899

Questions connexes