2013-09-25 5 views
1

Я делаю простое приложение для расчета с использованием пользовательского ввода, но у меня возникают проблемы с использованием класса Math для выполнения вычислений. Компилятор сообщает мнеошибка при попытке передать переменную и перейти к новому действию

не отлиты из EditText удвоить

, которые мне нужно сделать для того, чтобы использовать atan и pow математические функции.

Вторая вещь, о которой я не уверен, - это показать вычисления в моем editTexts с идентификаторами: mark1 и mark2.

Мой MainActivity выглядит следующим образом:

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; 

     } 

    } 

Это класс CalcResult, где я "попробовать", чтобы передать два результата расчета их 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"); 
    } 

} 

ответ

0

У вас есть эта

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); 

При извлечении у вас есть это

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

Изменения в

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

Update

Вы это

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

Тогда

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

Вы даже не получаете значение от editext.

В по щелчку

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

Теперь преобразовать строку в два раза поплавка согласно вашему требованию

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

хорошо, так что это должно быть так: 'двойной mark1 = bundle.getDouble (» marking1 ");' но что еще я делаю неправильно с вычислениями ?, так как я получаю ошибки в литье. – user2815899

+0

@ user2815899 вы должны получить текст из editext и преобразовать его в желаемый формат. – Raghunandan

+0

специально 'can can cast from editText to Double.' – user2815899

Смежные вопросы