2011-07-28 9 views
13

EditTextからdouble値を取得し、別のIntentにそれらを渡す前に操作します。プリミティブデータ型を使用しないので、toStringメソッドを使用できます。Androidで文字列をdoubleに変換する

問題は、私がprotein = Double.valueOf(p).doubleValue()をインクルードするときです。私はそれらをコメントアウトし、protein = 1.0のようないくつかのダミーデータを設定します。問題なく動作します。 同じことがプリミティブなデータ型と解析doubleで起こります。このコードは、通常のJavaのダミーデータと完全に動作します。 私は何が間違っていますか?

EditText txtProt, txtCarb, txtFat, txtFiber, txtPoints; 
String p, c, f, fi; 
Double protein, carbs, fat, fiber; 
double temp; 
Integer points; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.v("Create Prompt", "ready for layout"); 
    setContentView(R.layout.main); 
    Log.v("Layout Created", "ready for variable assignment"); 
    txtProt = (EditText) findViewById(R.id.Protein); 
    txtCarb = (EditText) findViewById(R.id.Carbs); 
    txtFat = (EditText) findViewById(R.id.Fat); 
    txtFiber = (EditText) findViewById(R.id.Fiber); 
    txtPoints = (EditText) findViewById(R.id.Points); 
    btnCalc = (Button) findViewById(R.id.Calc); 
    Log.v("Variables Assigned", "ready for double assignment"); 

    p = txtProt.getText().toString(); 
    c = txtCarb.getText().toString(); 
    f = txtFat.getText().toString(); 
    fi = txtFiber.getText().toString(); 


    protein=Double.valueOf(p).doubleValue(); 
    carbs=Double.valueOf(c).doubleValue(); 
    fat=Double.valueOf(f).doubleValue(); 
    fiber=Double.valueOf(fi).doubleValue(); 
    Log.v("Doubles parsed", "ready for calculations"); 
    //these are the problem statements 

    protein = 1.0; 
    carbs = 1.0; 
    fat = 1.0; 
    fiber = 1.0; 

    protein *= 16; 
    carbs *= 19; 
    fat *= 45; 
    fiber *= 14; 

    temp = protein + carbs + fat - fiber; 
    temp = temp/175; 

    points = new Integer((int) temp); 
+0

- のgetText()のTextViewが表示されるテキストを返します。別のバッファタイプでsetTextを呼び出していない限り、http://developer.android.com/reference/android/widget/EditText.html#getText() – Idistic

答えて

2

Double(String)コンストラクタの使用についてはどうですか?したがって、

protein = new Double(p); 

理由は分かりませんが、ショットに値する可能性があります。

+0

また、そこのどこにでもNumberFormatExeptionをキャッチしていないことが原因で問題が発生している可能性があります。ちょうどその場合にもそれを捕まえてログに記録したいかもしれません。 – MikeTheReader

+0

NumberFormatExceptionをキャッチしないと、プログラムを開いていなくても強制的に強制終了するのはなぜですか? – ProfCommie

+0

NumberFormatExceptionがどこかにある場合は、呼び出しスタックを制御しないコードに投げることになります。それを捕まえて、それが何を意味するのかを知っている場所で、特にユーザー入力を扱うときは、それを処理する方がよいでしょう。 – MikeTheReader

4

doubleオブジェクトをネイティブdouble valueフィールドに割り当てているようです。それは本当にコンパイルされますか?

Double.valueOf()はのdoubleオブジェクトを作成します。したがって、.doubleValue()は必要ありません。あなたはネイティブダブルフィールドをしたい場合は

は、あなたが.doubleValueをダブルとしてフィールドを定義して使用する必要があります()私はこのようにそれを行うだろう

+0

はい、コンパイルします。私はdoubleの代わりにDoubleを使用しているので、結果をintent.putExtra(string、string)を使って別のアクティビティに渡すことができます。 – ProfCommie

+1

@jkj:オートボクシングと呼ばれ、Javaの文書化された機能です。 (編集:あなたのコメントの場合はむしろ、自動アンボンシング) – Izkata

56

try { 
    txtProt = (EditText) findViewById(R.id.Protein); // Same 
    p = txtProt.getText().toString(); // Same 
    protein = Double.parseDouble(p); // Make use of autoboxing. It's also easier to read. 
} catch (NumberFormatException e) { 
    // p did not contain a valid double 
} 

EDITを: "プログラム強制は、ログカットの情報を残さずにすぐに閉じる"

私はlogcatの出力に情報を残していないが、私は強制的に一般的に私を知らないansには、NumberFormatExceptionのようなキャッチされない例外があります。

1

私にも同様の問題があります。私はこのコードを使用したdouble値に正しい書式のEditTextテキストコンテンツのための :

try { 
     String eAm = etAmount.getText().toString(); 
     DecimalFormat dF = new DecimalFormat("0.00"); 
     Number num = dF.parse(eAm); 
     mPayContext.amount = num.doubleValue(); 
    } catch (Exception e) { 
     mPayContext.amount = 0.0d; 
    } 

これは、現在の携帯電話のロケールからindependet、正しいdouble値を返しています。

願っています。

double d= Double.parseDouble(yourString); 
-2
String sc1="0.0"; 
Double s1=Double.parseDouble(sc1.toString()); 
+1

あなたの答えを書式化してください –

7

はこれを試して

  • のonCreateメソッドでのEditText値を解析を引き起こしましたアプリがクラッシュするのは、アプリが起動したときに、解析する値がないか、または文字であるプレースホルダがあるためです。

マイコード:ドキュメントから

package com.example.herodav.volumeapp; 

import android.renderscript.Double2; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.*; 
import android.widget.*; 

import org.w3c.dom.Text; 

public class MainActivity extends AppCompatActivity { 

    EditText height, length, depth; 
    TextView volume; 
    double h,l,d,vol; 


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

     height = (EditText)findViewById(R.id.h); 
     length = (EditText)findViewById(R.id.l); 
     depth = (EditText)findViewById(R.id.d); 
     volume = (TextView)findViewById(R.id.v); 

     Button btn = (Button)findViewById(R.id.btn); 
     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       calculateVolume(); 
       volume.setText("Volume = " + String.valueOf(vol)); 
      } 
     }); 
    } 

    public void calculateVolume(){ 
     h = Double.parseDouble(height.getText().toString()); 
     l = Double.parseDouble(length.getText().toString()); 
     d = Double.parseDouble(depth.getText().toString()); 
     vol = h*l*d; 
    } 
} 

私は

1
kw=(EditText)findViewById(R.id.kw); 
    btn=(Button)findViewById(R.id.btn); 
    cost=(TextView)findViewById(R.id.cost); 


      btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { cst = Double.valueOf(kw.getText().toString()); 
      cst = cst*0.551; 
      cost.setText(cst.toString()); 
     } 
    }); 
1

が、私は同じ問題を持っていましたが、私はちょうどことを考え出した:

関連する問題