久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在Android中將字符串轉(zhuǎn)換為雙精度

Converting String to Double in Android(在Android中將字符串轉(zhuǎn)換為雙精度)
本文介紹了在Android中將字符串轉(zhuǎn)換為雙精度的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

嘗試從 EditText 獲取雙精度值并在將它們傳遞給另一個 Intent 之前對其進行操作.不使用原始數(shù)據(jù)類型,所以我可以使用 toString 方法.

Trying to get double values from an EditText and manipulate them before passing them to another Intent. Not using primitive data type so I can use toString methods.

問題是當(dāng)我包含 protein=Double.valueOf(p).doubleValue();樣式命令,程序強制立即關(guān)閉,而不會在 logcat 中留下任何信息.如果我將它們注釋掉并設(shè)置一些虛擬數(shù)據(jù),例如 protein = 1.0;它沒有問題.原始數(shù)據(jù)類型和解析雙精度也會發(fā)生同樣的情況.此代碼與普通 java 中的虛擬數(shù)據(jù)完美配合.我做錯了什么?

Problem is when I include the protein=Double.valueOf(p).doubleValue(); style commands, the program force closes immediately without leaving any info in the logcat.If I comment them out and set some dummy data like protein = 1.0; it works with no problems. Same happens with primitive data types and parse double. This code works perfectly with dummy data in normal java. What am I doing wrong?

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

推薦答案

我會這樣做:

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
}

程序強制立即關(guān)閉,而不會在 logcat 中留下任何信息"

"the program force closes immediately without leaving any info in the logcat"

我不知道是否不會在 logcat 輸出中留下信息,但強制關(guān)閉通常意味著存在未捕獲的異常 - 例如 NumberFormatException.

I don't know bout not leaving information in the logcat output, but a force-close generally means there's an uncaught exception - like a NumberFormatException.

這篇關(guān)于在Android中將字符串轉(zhuǎn)換為雙精度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Cut, copy, paste in android(在android中剪切、復(fù)制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數(shù)的數(shù)字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 亚洲免费av一区 | 黄色操视频 | 麻豆视频国产在线观看 | 久在线精品视频 | 亚洲精品麻豆 | 久久精品中文 | 精品国产欧美 | 日韩免费av| 国产精品久久久久久久免费大片 | 91.色| 成人毛片视频免费 | 国产精品www | 在线欧美视频 | 国产乱码精品一品二品 | 国产精品精品久久久 | 精品成人 | 91文字幕巨乱亚洲香蕉 | 一级久久久久久 | 人人天天操| 99re国产视频 | 最新黄色在线观看 | 亚洲黄色视屏 | 99tv成人影院| 成人av观看| 亚州精品天堂中文字幕 | 国产免费观看一区 | 国产精品一区一区三区 | 免费成人av | 国产在线一区观看 | www国产亚洲精品 | av影音资源| 久久精品国产99国产 | 国产精品视频 | 国产精品视频免费观看 | 久久久久久久电影 | av片网| 欧美日韩免费 | 国产精品久久久久久吹潮 | 日韩精品免费在线观看 | 精品一区二区三区在线观看国产 | 亚洲国产aⅴ成人精品无吗 综合国产在线 |